from django.forms import model_to_dict
from django.http import HttpResponse, Http404
import os
from django.db.models import Q
from statsmodels.stats.multitest import multipletests
import numpy as np
import csv
from django.shortcuts import render
# from rest_framework.pagination import PageNumberPagination
# Create your views here.
from rest_framework.response import Response
from rest_framework.utils import json

from DB_demo.models import Gwas #引入模型
# from DB_demo.models import MrResult  #引入模型
# from DB_demo.models import MrResult1E5  #引入模型
# from DB_demo.models import MrResult5E8  #引入模型
from DB_demo.models import MrResultAll58465E8  #引入模型
from DB_demo.models import Papers  #引入模型
from DB_demo.models import PaperNew
from DB_demo.models import Mrdis2Dis1E5
from DB_demo.models import Mrdis2Dis5E6
from DB_demo.models import Mrdis2Dis5E8
from DB_demo.models import Mreqtl2Dis1E5
from DB_demo.models import Mreqtl2Dis5E6
from DB_demo.models import Mreqtl2Dis5E8
from DB_demo.models import Mrpqtl2Dis1E5
from DB_demo.models import Mrpqtl2Dis5E6
from DB_demo.models import Mrpqtl2Dis5E8
from DB_demo.models import Pubdis2Dis
from DB_demo.models import Pubeqtl2Dis
from DB_demo.models import Pubpqtl2Dis, Cpgsitename
from DB_demo.models import Mrall, MrallDisdis, MrallPqtldis, MrallEqtldis, MrallCombine, AllexposureName, AlloutcomeName
from DB_demo.models import Disname, DisnameOutcome, DownloadpageSingle
from DB_demo.models import Genename, GenenameOutcome, Metabolitename
from DB_demo.models import Proteinname, ProteinnameOutcome
from DB_demo.models import RsidPos, Phewas

from django.http import HttpResponse
from rest_framework import viewsets
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from DB_demo.serializer import GwasSerializer,  CpgsitenameSerializer #2

from DB_demo.serializer import MrResultAll58465E8Serializer, DownpagesingleSerializer, MetabolitenameSerializer
# from DB_demo.serializer import MrResultSerializer
from DB_demo.serializer import PapersSerializer #3
from DB_demo.serializer import PapersNewSerializer #3

from DB_demo.serializer import Mrdis2Dis1E5Serializer
from DB_demo.serializer import Mrdis2Dis5E6Serializer
from DB_demo.serializer import Mrdis2Dis5E8Serializer

from DB_demo.serializer import Mreqtl2Dis1E5Serializer
from DB_demo.serializer import Mreqtl2Dis5E6Serializer
from DB_demo.serializer import Mreqtl2Dis5E8Serializer

from DB_demo.serializer import Mrpqtl2Dis1E5Serializer
from DB_demo.serializer import Mrpqtl2Dis5E6Serializer
from DB_demo.serializer import Mrpqtl2Dis5E8Serializer

from DB_demo.serializer import Pubdis2DisSerializer
from DB_demo.serializer import Pubeqtl2DisSerializer
from DB_demo.serializer import Pubpqtl2DisSerializer
from DB_demo.serializer import MrallSerializer, MrallDisdisSerializer, MrallPqtldisSerializer, MrallEqtldisSerializer,  MrallCombineSerializer # UniversalMrallSerializer
from DB_demo.serializer import DisnameSerializer, GenenameSerializer,  ProteinnameSerializer

from DB_demo.serializer import PhewasSerializer, Phewas2Serializer, AllexposureNameSerializer

# from rest_framework.views import APIView
from django.http import JsonResponse
import csv
import io
from io import StringIO
import pandas as pd
from scipy.stats import chi2, norm #new package 05 10
import pandas as pd
import numpy as np
from rest_framework import status
from .utils import fuzzy_match_strings, fuzzy_match_strings2, fuzzy_match_strings3, fuzzy_match_strings4
from django.views import View
from rest_framework import serializers
# class SmallResultsSetPagination(PageNumberPagination):
#     page_size = 5
#     page_size_query_param = 'page_size'
#     max_page_size = 100


class GwasViewSet(viewsets.ModelViewSet):
    queryset = Gwas.objects.all()
    serializer_class = GwasSerializer


# class DownloadpagesingleViewSet(viewsets.ModelViewSet):
#     queryset = DownloadpageSingle.objects.all()
#     serializer_class = DownpagesingleSerializer


# class MRresultsViewSet(viewsets.ModelViewSet):
#     queryset = MrResult.objects.all()
#     serializer_class = MrResultSerializer
#
#     def list(self, request):
#         keyword = request.GET.get('keyword', None)
#         queryset = self.get_queryset()
#
#         if keyword is not None:
#             queryset = queryset.filter(
#                 Q(rawid__icontains=keyword) |
#                 Q(outcome__icontains=keyword) |
#                 Q(exposure__icontains=keyword) |
#                 Q(method__icontains=keyword)
#             )
#
#         serializer = self.get_serializer(queryset, many=True)
#         return Response(serializer.data)


# class MRresults1E5ViewSet(viewsets.ModelViewSet):
#     queryset = MrResult1E5.objects.all()
#     serializer_class = MrResult1E5Serializer
#
#     def list(self, request):
#         keyword = request.GET.get('keyword', None)
#         queryset = self.get_queryset()
#
#         if keyword is not None:
#             queryset = queryset.filter(
#                 Q(rawid__icontains=keyword) |
#                 Q(outcome__icontains=keyword) |
#                 Q(exposure__icontains=keyword) |
#                 Q(method__icontains=keyword)
#             )
#
#         serializer = self.get_serializer(queryset, many=True)
#         return Response(serializer.data)


# class MRresults5E8ViewSet(viewsets.ModelViewSet):
#     queryset = MrResult5E8.objects.all()
#     serializer_class = MrResult5E8Serializer
#
#     def list(self, request):
#         keyword = request.GET.get('keyword', None)
#         queryset = self.get_queryset()
#
#         if keyword is not None:
#             queryset = queryset.filter(
#                 Q(rawid__icontains=keyword) |
#                 Q(outcome__icontains=keyword) |
#                 Q(exposure__icontains=keyword) |
#                 Q(method__icontains=keyword)
#             )
#
#         serializer = self.get_serializer(queryset, many=True)
#         return Response(serializer.data)


class MrResultAll58465E8ViewSet(viewsets.ModelViewSet):
    queryset = MrResultAll58465E8.objects.all()
    serializer_class = MrResultAll58465E8Serializer

    def list(self, request):
        keyword = request.GET.get('keyword', None)
        queryset = self.get_queryset()

        if keyword is not None:
            queryset = queryset.filter(
                Q(rawid__icontains=keyword) |
                Q(outcome__icontains=keyword) |
                Q(exposure__icontains=keyword) |
                Q(method__icontains=keyword)
            )

        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)


class PapersViewSet(viewsets.ModelViewSet):

    queryset = Papers.objects.all()
    serializer_class = PapersSerializer
    # pagination_class = SmallResultsSetPagination

    def get_queryset(self):
        """
        Optionally restricts the returned purchases to a given user,
        by filtering against a `keyword` query parameter in the URL.
        """
        queryset = super().get_queryset()
        keyword = self.request.query_params.get('keyword', None)

        if keyword is not None:
            queryset = queryset.filter(
                Q(pmid__icontains=keyword) |
                Q(title__icontains=keyword) |
                Q(gpt__icontains=keyword)
            )

        return queryset


    # def get_queryset(self):
    #     # 如果需要，可以在这里进一步自定义查询集
    #     return PaperNew.objects.all()


class PaperNewViewSet(viewsets.ModelViewSet):
    queryset = PaperNew.objects.all().order_by('id')
    serializer_class = PapersNewSerializer

    def get_queryset(self):
        queryset = super().get_queryset()
        print(queryset.count())
        pmid = self.request.query_params.get('id_exposure', None)

        print("Received PMID:", pmid)

        if pmid:
            queryset = queryset.filter(id_exposure=pmid)
        return queryset
    # pagination_class = SmallResultsSetPagination  # 使用自定义的分页类


    # def list(self, request):
    #     keyword = request.GET.get('keyword', None)
    #     queryset = self.get_queryset()
    #
    #     if keyword is not None:
    #         queryset = queryset.filter(
    #             Q(pmid__icontains=keyword) |
    #             Q(title__icontains=keyword) |
    #             Q(gpt__icontains=keyword)
    #         )
    #
    #     serializer = self.get_serializer(queryset, many=True)
    #     return Response(serializer.data)

# def get_paper(request, paper_id):
#     # 根据传入的paper_id从数据库中获取对应的Paper对象
#     print('here get paper id=', paper_id)
#     paper = PaperNew.objects.get(id=paper_id)
#     # 将Paper对象转换为字典格式
#     paper_dict = {
#         "title": paper.title,
#         "p_value_threshold": paper.p_value_threshold,
#         "r2": paper.r2,
#         "clumping_window_kb_field": paper.clumping_window_kb_field,
#         "f_statistic": paper.f_statistic,
#         "p_excluded_the_ivs_showing_solid_correlations_with_outcomes": paper.p_excluded_the_ivs_showing_solid_correlations_with_outcomes,
#         "gpt": paper.gpt,
#         "if_picture": paper.if_picture,
#         "figure_name": paper.figure_name,
#         "legends": paper.legends,
#         "pmid": paper.pmid,
#         "showpic": paper.showpic,
#         "index": paper.index,
#     }
#     # 返回Json格式的响应
#     return JsonResponse(paper_dict)


def findRSID(line):
    one = line.split(':')
    chr = one[0]  # 第一个字符串
    pos = one[1]  # 第二个字符串
    queryset = RsidPos.objects.filter(Q(chr=chr) & Q(pos38=pos)).values_list('rsid', flat=True).first()

    return queryset

def findChrPos_bulk(chrs_and_poss):
    # 使用 `__in` 批量查询

    queryset = RsidPos.objects.filter(
        Q(rsid__in=[item for item in chrs_and_poss])
    ).values_list('ref', 'alt', 'rsid', 'pos19', 'pos38', 'chr')

    rsid_dict = {rsid: (ref, alt, chr, pos38, pos19) for ref, alt, rsid, pos19, pos38, chr in queryset}
    print("rsid_dict:", rsid_dict)
    return rsid_dict


def findRSID_bulk(chrs_and_poss, pos):
    # 使用 `__in` 批量查询
    if pos =='pos38':
        queryset = RsidPos.objects.filter(
            Q(chr__in=[item[0] for item in chrs_and_poss]) &
            Q(pos38__in=[item[1] for item in chrs_and_poss])
        ).values_list('ref', 'alt', 'chr', 'pos38', 'rsid')
    else:
        queryset = RsidPos.objects.filter(
            Q(chr__in=[item[0] for item in chrs_and_poss]) &
            Q(pos19__in=[item[1] for item in chrs_and_poss])
        ).values_list('ref', 'alt', 'chr', 'pos19', 'rsid')

    rsid_dict = {(chr, pos): (rsid, ref, alt) for ref, alt, chr, pos, rsid in queryset}
    #print("rsid_dict:", rsid_dict)
    return rsid_dict


def upload_csv_one(request):
    if request.method == 'POST':
        file = request.FILES.get('file')
        file_wrapper = io.TextIOWrapper(file, encoding='utf-8')
        reader = csv.reader(file_wrapper)
        data_lines = []
        for line in reader:
            one = line[0].replace('\ufeff', '')
            data_lines.append(one)
        # print('data_lines:', data_lines)
        #chrs_and_poss = [tuple(line.split(':')) for line in data_lines]
        #print('chrs_and_poss:', chrs_and_poss)
        rsid_dict = findChrPos_bulk(data_lines)
        # print('rsid_dict:', rsid_dict)
        result = []
        for line in data_lines:
            ref, alt, chr, pos38, pos19 = rsid_dict.get((line))
            if pos38:
                result.append({'input': line, 'Chr': chr, 'GRCh38': pos38, 'GRCh37': pos19, 'REF': ref, 'ALT': alt})

        # print('result:', result)
        return JsonResponse(result, safe=False)


def upload_csv(request):
    if request.method == 'POST':
        # print("request:", request)
        file = request.FILES.get('file')
        #print('file:', file)
        pos = request.POST.get('pos', '')
       # print('pos:', pos)
        file_wrapper = io.TextIOWrapper(file, encoding='utf-8')
        reader = csv.reader(file_wrapper)
        # print("reader:", reader)
        # numbers = []
        data_lines = []
        for line in reader:
           # print(line[0])
            line[0] = line[0].replace('\ufeff', '')
            data_lines.append(line[0])

        chrs_and_poss = [tuple(line.split(':')) for line in data_lines]
        #print('chrs_and_poss:', chrs_and_poss)
        rsid_dict = findRSID_bulk(chrs_and_poss, pos)
        # print('rsid_dict:', rsid_dict)

        result = []
        for line in data_lines:
            chr, pos = line.split(':')
            rsid, ref, alt = rsid_dict.get((int(chr), int(pos)))
            if rsid:
                result.append({'input': line, 'rsids': rsid, 'REF': ref, 'ALT': alt})

        # for line in reader:
        #     # 按照":"分割数据行
        #     line = line[0]
        #
        #     b2_data = findRSID(line)
        #     if b2_data:
        #         result.append({'raw': line, 'rsids': b2_data})
        # print('result:', result)
        return JsonResponse(result, safe=False)


def cal_F(df):
    df['R2'] = 2 * (1 - df['EAF'].astype(float)) * df['EAF'].astype(float) * df['Beta'].astype(float) ** 2
    df['F-static'] = (df['R2'] / (1 - df['R2'])) * (df['Samplesize'].astype(float) - 2)
    df = df.drop('R2', axis=1)
    return df


def upload_csv_F(request):
    if request.method == 'POST':
        #file = request.FILES.get('file')
        #data = request.body

       # print('------------')
        file = request.FILES.get('file')
        file_wrapper = io.TextIOWrapper(file, encoding='utf-8')
        reader = csv.reader(file_wrapper)
        header = next(reader)
        df = pd.DataFrame(columns=header)
        for line in reader:
            row = pd.DataFrame([line], columns=header)
            df = pd.concat([df, row], ignore_index=True)
        # numbers = []
        df = cal_F(df)
        processed_data = df.to_json(orient='records')
        return JsonResponse(processed_data, safe=False)


def testupdate(request):
    if request.method == 'POST':
        #data2 = request.POST.get('test', '')
        #data2 = request.Post.d('test')
        data = json.loads(request.body)
       # print(data)
       # print('------------')
        df = pd.read_csv(StringIO(data['text']), sep='\t', header=0)
       # print(df)
       # print('-----------')
       # print(df['EAF'])
        df = cal_F(df)
        processed_data = df.to_json(orient='records')
        return JsonResponse(processed_data, safe=False)
        #return HttpResponse('get it!')


def results_binary(N, alpha, R2xz, K, OR, epower):

    if epower is None:
        threschi = chi2.ppf(1 - alpha, 1)  # threshold chi(1) scale
        f_value = 1 + N * R2xz / (1 - R2xz)
        b_MR = K * (OR / (1 + K * (OR - 1)) - 1)
        v_MR = (K * (1 - K) - b_MR ** 2) / (N * R2xz)
        NCP = b_MR ** 2 / v_MR

        # 2-sided test
        power = 1 - chi2.cdf(threschi, 1, NCP)
        return pd.DataFrame({
            "Parameter": ["Power", "NCP", "F-statistic"],
            "Value": [power, NCP, f_value],
            "Description": ["", "Non-Centrality-Parameter", "The strength of the instrument"]
        })
    else:
        # Calculation of sample size given power
        z1 = norm.ppf(1 - alpha / 2)
        z2 = norm.ppf(epower)
        Z = (z1 + z2) ** 2

        b_01 = K * (OR / (1 + K * (OR - 1)) - 1)
        f = K * (1 - K) - b_01 ** 2
        N1 = Z * f / (b_01 ** 2 * R2xz)
        N1 = np.ceil(N1)
        return pd.DataFrame({
            "Parameter": ["Sample Size"],
            "Value": [N1]
        })


def calPower(request):
    if request.method == 'POST':
        data = json.loads(request.body)
        form = data.get('form')

        want = form.get('want')
        alpha = float(form.get('significanceLevel'))
        print(int(form.get('sampleSize')))
        try:
            N = None if want != 'Power' else int(form.get('sampleSize'))
            print(N)
        except (TypeError, ValueError):
            return HttpResponse('Invalid sampleSize.', status=400)

        R2xz = float(form.get('R2'))
        print(R2xz)
        K = float(form.get('K'))
        OR = float(form.get('OR'))
        epower = None if want == 'Power' else float(form.get('Power'))

        # 调用函数并传入正确的参数
        df = results_binary(N, alpha, R2xz, K, OR, epower)

        # 将 DataFrame 转换为 JSON 并返回
        return JsonResponse(df.to_dict(orient='records'), safe=False)

    return HttpResponse('This endpoint only supports POST requests.', status=405)


# def process_data(request):
#     if request.method == 'POST':
#         req = json.loads(request.body)
#         data = req.get('test', '')
#         data_lines = data.strip().split('\n')
#         print(data_lines)
#         # 逐行处理数据
#         result = []
#         for line in data_lines:
#
#             # 根据position列匹配获取rsid列的值
#             print('---------')
#             print(line)
#             print('---------')
#             b2_data = findRSID(line)
#             print(b2_data)
#             if b2_data:
#                 result.append({'input': line, 'rsids': b2_data})
#             print(result)
#
#         return JsonResponse(result, safe=False)
def process_data_one(request):
    if request.method == 'POST':
        req = json.loads(request.body)
        data = req.get('test', '')
       # print('data:', data)
        data_lines = data.strip().split('\n')
      #  print("data_lines:", data_lines)
        # 准备批量查询
        rsid_dict = findChrPos_bulk(data_lines)
        #print('rsid_dict:', rsid_dict)

        result = []
        for line in data_lines:
            ref, alt,  chr, pos38, pos19 = rsid_dict.get((line))
            if pos38:
                result.append({'input': line, 'Chr': chr, 'GRCh38': pos38, 'GRCh37': pos19, 'REF': ref, 'ALT': alt})

       # print('result:', result)
        return JsonResponse(result, safe=False)


def process_data(request):
    if request.method == 'POST':
        req = json.loads(request.body)
        data = req.get('test', '')
        pos = req.get('type', '')
       # print('pos:', pos)
        data_lines = data.strip().split('\n')
       # print(data_lines)
        # 准备批量查询
        chrs_and_poss = [tuple(line.split(':')) for line in data_lines]
       # print('chrs_and_poss:', chrs_and_poss)
        rsid_dict = findRSID_bulk(chrs_and_poss, pos)
        #print('rsid_dict:', rsid_dict)

        result = []
        for line in data_lines:
            chr, pos = line.split(':')
            rsid, ref, alt = rsid_dict.get((int(chr), int(pos)))
            if rsid:
                result.append({'input': line, 'rsids': rsid, 'REF': ref, 'ALT': alt})
            # rsid = rsid_dict.get((int(chr), int(pos)))
            # if rsid:
            #     result.append({'input': line, 'rsids': rsid})

      #  print('result:', result)
        return JsonResponse(result, safe=False)

# class Mrdis2Dis1E5ViewSet(viewsets.ModelViewSet):
#     queryset = Mrdis2Dis1E5.objects.all()
#     serializer_class = Mrdis2Dis1E5Serializer

def get_paginated_results(page=1, items_per_page=10, all_results=Mrall):

    # 创建Paginator对象
    paginator = Paginator(all_results, items_per_page)

    try:
        # 获取请求页码的数据
        results = paginator.page(page)
    except PageNotAnInteger:
        # 如果页码不是整数，则返回第一页
        results = paginator.page(1)
    except EmptyPage:
        # 如果页码超出范围，则返回最后一页的结果
        results = paginator.page(paginator.num_pages)

    # 序列化数据并返回响应
    data = [item.serialize() for item in results]  # 假设你有一个序列化方法
    response_data = {
        'data': data,
        'total': paginator.count,
        'num_pages': paginator.num_pages,
        'current_page': page
    }
    return JsonResponse(response_data)


class Mrdis2Dis1E5ViewSet(viewsets.ModelViewSet):
    serializer_class = Mrdis2Dis1E5Serializer

    # def get_queryset(self):
    #     queryset = Mrdis2Dis1E5.objects.all()
    #     all_params = self.request.query_params
    #     print('all_params:', all_params)
    #     for key, value in self.request.query_params.items():
    #         print(f"Parameter: {key}, Value: {value}")
    #     names = self.request.query_params.get('names', None)
    #     page = self.request.query_params.get('page', None)
    #     page = int(page)
    #     print('page:', page)
    #     if names is not None:
    #         queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
    #
    #     #queryset = get_paginated_results()
    #     paginator = Paginator(queryset, 9)
    #     results = paginator.page(page)
    #     print(results)
    #     response_data = {
    #         'data': results,
    #         'total': paginator.count,
    #         'num_pages': paginator.num_pages,
    #         'current_page': page
    #     }
    #
    #     return response_data


class Mrdis2Dis1E5ViewSet(viewsets.ModelViewSet):
    serializer_class = Mrdis2Dis1E5Serializer

    def get_queryset(self):
        queryset = Mrdis2Dis1E5.objects.all().order_by('id')
        names = self.request.query_params.get('names', None)
        if names is not None:
            queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
        return queryset

    def list(self, request, *args, **kwargs):
        page = request.query_params.get('page', 1)
        perPage_items = request.query_params.get('perPage_items', 10)
        try:
            page = int(page)
        except ValueError:
            page = 1
        try:
            perPage_items = int(perPage_items)
        except ValueError:
            page = 10

        queryset = self.get_queryset()
        paginator = Paginator(queryset, perPage_items)
        try:
            results = paginator.page(page)
        except EmptyPage:
            results = []

        serializer = self.get_serializer(results, many=True)
        response_data = {
            'data': serializer.data,
            'total': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page
        }
        return Response(response_data)


class Mrdis2Dis5E6ViewSet(viewsets.ModelViewSet):
    serializer_class = Mrdis2Dis5E6Serializer

    def get_queryset(self):
        queryset = Mrdis2Dis5E6.objects.all()
        names = self.request.query_params.get('names', None)
        print('names:', names)
        if names:
            queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
        return queryset


class Mrdis2Dis5E8ViewSet(viewsets.ModelViewSet):
    serializer_class = Mrdis2Dis5E8Serializer

    def get_queryset(self):
        queryset = Mrdis2Dis5E8.objects.all()
        names = self.request.query_params.get('names', None)
        if names:
            queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
        return queryset


class Mreqtl2Dis1E5ViewSet(viewsets.ModelViewSet):
    serializer_class = Mreqtl2Dis1E5Serializer

    def get_queryset(self):
        queryset = Mreqtl2Dis1E5.objects.all()
        names = self.request.query_params.get('names', None)
        if names:
            queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
        return queryset


class Mreqtl2Dis5E6ViewSet(viewsets.ModelViewSet):
    serializer_class = Mreqtl2Dis5E6Serializer

    def get_queryset(self):
        queryset = Mreqtl2Dis5E6.objects.all()
        names = self.request.query_params.get('names', None)
        if names:
            queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
        return queryset


class Mreqtl2Dis5E8ViewSet(viewsets.ModelViewSet):
    serializer_class = Mreqtl2Dis5E8Serializer

    def get_queryset(self):
        queryset = Mreqtl2Dis5E8.objects.all()
        names = self.request.query_params.get('names', None)
        if names:
            queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
        return queryset


class Mrpqtl2Dis1E5ViewSet(viewsets.ModelViewSet):
    serializer_class = Mrpqtl2Dis1E5Serializer

    def get_queryset(self):
        queryset = Mrpqtl2Dis1E5.objects.all()
        names = self.request.query_params.get('names', None)
        if names:
            queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
        return queryset


class Mrpqtl2Dis5E6ViewSet(viewsets.ModelViewSet):
    serializer_class = Mrpqtl2Dis5E6Serializer

    def get_queryset(self):
        queryset = Mrpqtl2Dis5E6.objects.all()
        names = self.request.query_params.get('names', None)
        if names:
            queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
        return queryset


class Mrpqtl2Dis5E8ViewSet(viewsets.ModelViewSet):
    serializer_class = Mrpqtl2Dis5E8Serializer

    def get_queryset(self):
        queryset = Mrpqtl2Dis5E8.objects.all()
        names = self.request.query_params.get('names', None)
        if names:
            queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
        return queryset


class Pubeqtl2DisViewSet(viewsets.ModelViewSet):
    serializer_class = Pubeqtl2DisSerializer

    def get_queryset(self):
        queryset = Pubeqtl2Dis.objects.all()
        names = self.request.query_params.get('names', None)
        if names:
            queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
        return queryset


class Pubdis2DisViewSet(viewsets.ModelViewSet):
    serializer_class = Pubdis2DisSerializer

    def get_queryset(self):
        queryset = Pubdis2Dis.objects.all()
        names = self.request.query_params.get('names', None)
        if names:
            queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
        return queryset


class Pubpqtl2DisViewSet(viewsets.ModelViewSet):
    serializer_class = Pubpqtl2DisSerializer

    def get_queryset(self):
        queryset = Pubpqtl2Dis.objects.all()
        names = self.request.query_params.get('names', None)
        if names:
            queryset = queryset.filter(Q(exposure=names) | Q(outcome=names))
        return queryset


def get_serializer_for_model(model):
    # 根据模型返回对应的序列化器
    if model == Mrdis2Dis1E5:
        return Mrdis2Dis1E5Serializer
    elif model == Mrdis2Dis5E6:
        return Mrdis2Dis5E6Serializer
    elif model == Mrdis2Dis5E8:
        return Mrdis2Dis5E8Serializer
    elif model == Mreqtl2Dis1E5:
        return Mreqtl2Dis1E5Serializer
    elif model == Mreqtl2Dis5E6:
        return Mreqtl2Dis5E6Serializer
    elif model == Mreqtl2Dis5E8:
        return Mreqtl2Dis5E8Serializer
    elif model == Mrpqtl2Dis1E5:
        return Mrpqtl2Dis1E5Serializer
    elif model == Mrpqtl2Dis5E6:
        return Mrpqtl2Dis5E6Serializer
    elif model == Mrpqtl2Dis5E8:
        return Mrpqtl2Dis5E8Serializer
    elif model == Pubdis2Dis:
        return Pubdis2DisSerializer
    elif model == Pubeqtl2Dis:
        return Pubeqtl2DisSerializer
    else:
        return Pubpqtl2DisSerializer

# browse页面
# class CombinedResultsView(viewsets.ModelViewSet):
#     serializer_class = MrallSerializer
#
#     def get_queryset(self):
#         query = Mrall.objects.all().order_by('id')
#         print('初始查询集数量：', query.count())
#         # 从请求中提取参数
#         source = self.request.query_params.getlist('resultTypes[]', None)
#         analysistype = self.request.query_params.getlist('type[]', None)
#         pvaluebefore = [self.request.query_params.get('pvalue', None)]
#         method = self.request.query_params.getlist('method[]', None)
#         pvalresult = self.request.query_params.get('pvalresult', None)
#         disnames = self.request.query_params.get('names', None)
#         if 'B' in source:
#             pvaluebefore.append('Pub')
#         print('参数：', source, analysistype, pvaluebefore, disnames)
#         # 根据提供的参数构建查询
#
#         conditions = Q()
#
#         # 应用任何额外的过滤条件（例如，基于 names）
#         if source:
#             conditions &= Q(source__in=source)
#
#         if pvaluebefore:
#             conditions &= Q(pvaluebefore__in=pvaluebefore)
#         if method:
#             conditions &= Q(method__in=method)
#         if analysistype:
#             conditions &= Q(analysistype__in=analysistype)
#
#         if pvalresult:
#             pval_float = float(pvalresult)
#            # print("pval_float:", pval_float)
#             conditions &= Q(pval__lt=pval_float)
#
#         if disnames:
#             conditions &= Q(exposure=disnames) | Q(outcome=disnames)
#
#         if conditions:
#             print('conditions:', conditions)
#             query = query.filter(conditions)
#
#         return query
#
#     def list(self, request, *args, **kwargs):
#
#         page = self.request.query_params.get('page', 1)
#         perPage_items = self.request.query_params.get('perPage_items', 9)
#
#         print('分页参数：', page, perPage_items)
#         try:
#             page = int(page)
#         except ValueError:
#             page = 1
#         try:
#             perPage_items = int(perPage_items)
#         except ValueError:
#             perPage_items = 9
#
#         query = self.get_queryset()
#         # for item in query.values():
#         #     print('值：', item)
#
#         paginator = Paginator(query, perPage_items)
#         try:
#             results = paginator.page(page)
#         except EmptyPage:
#             results = []
#
#         serializer = self.get_serializer(results, many=True)
#         # 使用序列化器序列化查询结果
#
#         response_data = {
#             'data': serializer.data,
#             'total': paginator.count,
#             'num_pages': paginator.num_pages,
#             'current_page': page
#         }
#         return Response(response_data)

class CombinedResultsView(viewsets.ModelViewSet):
   # serializer_class = MrallSerializer
   def get_serializer_class(self):
       sources = self.request.query_params.get('type', None)
       if sources == "A":
           analysistype = self.request.query_params.get('type', None)
           print('analysistype:', analysistype)
           if analysistype == 'disdis':
               return MrallDisdisSerializer
           elif analysistype == 'pqtldis':
               return MrallPqtldisSerializer
           elif analysistype == 'eqtldis':
               return MrallEqtldisSerializer
           else:
               raise Http404("Invalid analysistype parameter")
       else:
           analysistype = self.request.query_params.get('type', None)
           print('analysistype:', analysistype)
           if analysistype == 'disdis':
               return MrallDisdisSerializer
           elif analysistype == 'pqtldis':
               return MrallPqtldisSerializer
           elif analysistype == 'eqtldis':
               return MrallEqtldisSerializer
           else:
               raise Http404("Invalid analysistype parameter")

   def get_queryset(self):
        sources = self.request.query_params.get('resultTypes', None)
        if sources == "A":
            analysistype = self.request.query_params.get('type', None)
            if analysistype == 'disdis':
                queryset = MrallDisdis.objects.all().order_by('id')
            elif analysistype == 'pqtldis':
                queryset = MrallPqtldis.objects.all().order_by('id')
            else:
                queryset = MrallEqtldis.objects.all().order_by('id')
        else:
            analysistype = self.request.query_params.get('analysistype', None)
            if analysistype == 'disdis':
                queryset = MrallDisdis.objects.all().order_by('id')
            elif analysistype == 'pqtldis':
                queryset = MrallPqtldis.objects.all().order_by('id')
            else:
                queryset = MrallEqtldis.objects.all().order_by('id')

        print('初始查询集数量：', queryset.count())
        # 从请求中提取参数
        #source = self.request.query_params.getlist('resultTypes[]', None)
        #analysistype = self.request.query_params.getlist('type[]', None)
        #pvaluebefore = [self.request.query_params.get('pvalue', None)]
        method = self.request.query_params.getlist('method[]', None)
        pvalresult = self.request.query_params.get('pvalresult', None)
        disnames = self.request.query_params.get('names', None)
        # if 'B' in source:
        #     pvaluebefore.append('Pub')
        # print('参数：', source, analysistype, pvaluebefore, disnames)
        # 根据提供的参数构建查询

        conditions = Q()

        # 应用任何额外的过滤条件（例如，基于 names）
        # if source:
        #     conditions &= Q(source__in=source)
        #
        # if pvaluebefore:
        #     conditions &= Q(pvaluebefore__in=pvaluebefore)
        if method:
            conditions &= Q(method__in=method)
        # if analysistype:
        #     conditions &= Q(analysistype__in=analysistype)

        if pvalresult:
            pval_float = float(pvalresult)
           # print("pval_float:", pval_float)
            conditions &= Q(pval__lt=pval_float)

        if disnames:
            conditions &= Q(exposure=disnames) | Q(outcome=disnames)

        if conditions:
            print('conditions:', conditions)
            queryset = queryset.filter(conditions)

        return queryset

   def list(self, request, *args, **kwargs):

        page = self.request.query_params.get('page', 1)
        perPage_items = self.request.query_params.get('perPage_items', 10)

        print('分页参数：', page, perPage_items)
        try:
            page = int(page)
        except ValueError:
            page = 1
        try:
            perPage_items = int(perPage_items)
        except ValueError:
            perPage_items = 10

        query = self.get_queryset()
        # # for item in query.values():
        # #     print('值：', item)
        #
        # paginator = Paginator(query, perPage_items)
        # try:
        #     results = paginator.page(page)
        # except EmptyPage:
        #     results = []
        #
        # serializer = self.get_serializer(results, many=True)
        # # 使用序列化器序列化查询结果
        #
        # response_data = {
        #     'data': serializer.data,
        #     'total': paginator.count,
        #     'num_pages': paginator.num_pages,
        #     'current_page': page
        # }
        # return Response(response_data)
   # 检查查询集是否为空
        if query.count() == 0:
            # 如果为空，则返回空数据和元数据
            return Response({
                'data': [],
                'total': 0,
                'num_pages': 0,
                'current_page': page
            }, status=status.HTTP_200_OK)

        paginator = Paginator(query, perPage_items)
        try:
            results = paginator.page(page)
        except EmptyPage:
            # 如果请求的页码超出范围，返回空数据和元数据
            results = []
            return Response({
                'data': [],
                'total': paginator.count,
                'num_pages': paginator.num_pages,
                'current_page': page
            }, status=status.HTTP_200_OK)

        serializer = self.get_serializer(results, many=True)

        response_data = {
            'data': serializer.data,
            'total': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page
        }
        return Response(response_data)

# class CombinedResultsView2(APIView):
#     def get(self, request, *args, **kwargs):
#         # 从请求中提取参数
#         result_types = request.query_params.getlist('resultTypes[]')
#         type = request.query_params.getlist('type[]')
#         pvalue = request.query_params.get('pvalue', '')
#         pvalresult = request.query_params.get('pvalresult', '')
#         names = request.query_params.get('names', '')
#
#         print('参数：', result_types, type, pvalue, names)
#         # 根据提供的参数构建查询
#         queries = []
#         if 'Our Analysis Results' in result_types and 'Dis-Dis' in type:
#
#             if pvalue == '1e-5':
#                 queries.append(Mrdis2Dis1E5.objects.all())
#                # print(' print(queries)', queries)
#             if pvalue == '5e-6':
#                # print('here ok')
#                 queries.append(Mrdis2Dis5E6.objects.all())
#                # print(queries)
#             if pvalue == '5e-8':
#                 queries.append(Mrdis2Dis5E8.objects.all())
#
#         if 'Our Analysis Results' in result_types and 'eQTL-Dis' in type:
#             if pvalue == '1e-5':
#                 queries.append(Mreqtl2Dis1E5.objects.all())
#             if pvalue == '5e-6':
#                 queries.append(Mreqtl2Dis5E6.objects.all())
#             if pvalue == '5e-8':
#                 queries.append(Mreqtl2Dis5E8.objects.all())
#
#         if 'Our Analysis Results' in result_types and 'pQTL-Dis' in type:
#             if pvalue == '1e-5':
#                 queries.append(Mrpqtl2Dis1E5.objects.all())
#             if pvalue == '5e-6':
#                 queries.append(Mrpqtl2Dis5E6.objects.all())
#             if pvalue == '5e-8':
#                 queries.append(Mrpqtl2Dis5E8.objects.all())
#
#         if 'Published Results' in result_types and 'Dis-Dis' in type:
#             print('Pub')
#             queries.append(Pubdis2Dis.objects.all())
#         if 'Published Results' in result_types and 'eQTL-Dis' in type:
#             queries.append(Pubeqtl2Dis.objects.all())
#         if 'Published Results' in result_types and 'pQTL-Dis' in type:
#             queries.append(Pubeqtl2Dis.objects.all())
#
#         combined_results = []
#         for query in queries:
#             # 应用任何额外的过滤条件（例如，基于 names）
#             # print('query:', query)
#             if pvalresult:
#                 pval_float = float(pvalresult)
#                 query = query.filter(Q(pval__lt=pval_float))
#
#             if names:
#                 print('here')
#                 #query = query.filter(exposure__icontains=names)
#                 query = query.filter(Q(exposure=names) | Q(outcome=names))
#
#             # 使用序列化器序列化查询结果
#             print('query.model:', query.model)
#             serializer = get_serializer_for_model(query.model)(query, many=True)
#             # print('serializer:', serializer)
#             combined_results.extend(serializer.data)


# class CombinedResultsView(APIView):
#     def get(self, request, *args, **kwargs):
#         # 从请求中提取参数
#         result_types = request.query_params.getlist('resultTypes[]')
#         type = request.query_params.getlist('type[]')
#         pvalue = request.query_params.get('pvalue', '')
#         pvalresult = request.query_params.get('pvalresult', '')
#         names = request.query_params.get('names', '')
#         page = request.query_params.get('page', 1)
#         perPage_items = request.query_params.get('perPage_items', 9)
#
#         try:
#             page = int(page)
#         except ValueError:
#             page = 1
#         try:
#             perPage_items = int(perPage_items)
#         except ValueError:
#             perPage_items = 9
#
#
#         print('参数：', result_types, type, pvalue, names, page, perPage_items)
#         # 根据提供的参数构建查询
#         queries = []
#         if 'Our Analysis Results' in result_types and 'Dis-Dis' in type:
#
#             if pvalue == '1e-5':
#                 queries.append(Mrdis2Dis1E5.objects.all())
#                # print(' print(queries)', queries)
#             if pvalue == '5e-6':
#                # print('here ok')
#                 queries.append(Mrdis2Dis5E6.objects.all())
#                # print(queries)
#             if pvalue == '5e-8':
#                 queries.append(Mrdis2Dis5E8.objects.all())
#
#         if 'Our Analysis Results' in result_types and 'eQTL-Dis' in type:
#             if pvalue == '1e-5':
#                 queries.append(Mreqtl2Dis1E5.objects.all())
#             if pvalue == '5e-6':
#                 queries.append(Mreqtl2Dis5E6.objects.all())
#             if pvalue == '5e-8':
#                 queries.append(Mreqtl2Dis5E8.objects.all())
#
#         if 'Our Analysis Results' in result_types and 'pQTL-Dis' in type:
#             if pvalue == '1e-5':
#                 queries.append(Mrpqtl2Dis1E5.objects.all())
#             if pvalue == '5e-6':
#                 queries.append(Mrpqtl2Dis5E6.objects.all())
#             if pvalue == '5e-8':
#                 queries.append(Mrpqtl2Dis5E8.objects.all())
#
#         if 'Published Results' in result_types and 'Dis-Dis' in type:
#             print('Pub')
#             queries.append(Pubdis2Dis.objects.all())
#         if 'Published Results' in result_types and 'eQTL-Dis' in type:
#             queries.append(Pubeqtl2Dis.objects.all())
#         if 'Published Results' in result_types and 'pQTL-Dis' in type:
#             queries.append(Pubeqtl2Dis.objects.all())
#
#         combined_results = []
#         for query in queries:
#             # 应用任何额外的过滤条件（例如，基于 names）
#             if pvalresult:
#                 pval_float = float(pvalresult)
#                 query = query.filter(Q(pval__lt=pval_float))
#
#             if names:
#                 query = query.filter(Q(exposure=names) | Q(outcome=names))
#
#             # 使用序列化器序列化查询结果
#                 # 在序列化之前进行分页
#             paginator = Paginator(query, perPage_items)
#             try:
#                 results = paginator.page(page)
#             except EmptyPage:
#                 results = []
#
#             # 使用序列化器序列化查询结果
#             serializer = get_serializer_for_model(query.model)(results.object_list, many=True)
#             combined_results.extend(serializer.data)
#
#         response_data = {
#             'data': combined_results,
#             'total': paginator.count,
#             'num_pages': paginator.num_pages,
#             'current_page': page
#         }
#         return Response(response_data)



# class CombinedViewSet(viewsets.ViewSet):
#     def list(self, request):
#         # 从查询参数中获取 sort 参数
#         sort = request.query_params.get('sort', None)
#
#         # 获取所有的数据
#         all_data = []
#         for model, serializer in [(Pubpqtl2Dis, Pubpqtl2DisSerializer), (Mrdis2Dis1E5, Mrdis2Dis1E5Serializer), (Mrdis2Dis5E6, Mrdis2Dis5E6Serializer), ...]:  # 列出所有的模型和序列化器
#             queryset = model.objects.all()
#             if sort is not None:
#                 queryset = queryset.order_by(sort)
#             serializer = serializer(queryset, many=True)
#             all_data.extend(serializer.data)
#
#         # 返回合并后的数据
#         return JsonResponse(all_data, safe=False)


# class MrallViewSet(viewsets.ModelViewSet):
#     queryset = Mrall.objects.all()
#     serializer_class = MrallSerializer


# class MrallViewSet(viewsets.ModelViewSet):
#     serializer_class = MrallSerializer
#
#     def get_queryset(self):
#         queryset = Mrall.objects.all()
#         # 获取请求中的查询参数
#         query = self.request.query_params.get('query', None)
#         print('query:', query)
#         #if query == '':
#         id = self.request.query_params.get('id', None)
#         # exposureType = self.request.query_params.get('exposureType', None)
#         exposure = self.request.query_params.getlist('exposure[]')
#         # print('exposure:', exposure)
#         id_exposure = self.request.query_params.get('id_exposure', None)
#         #print('id_exposure:', id_exposure)
#        # outcomeType = self.request.query_params.get('outcomeType', None)
#         outcome = self.request.query_params.get('outcome', None)
#         id_outcome = self.request.query_params.get('id_outcome', None)
#         analysistype = self.request.query_params.getlist('analysistype[]')  #getlist('analysistype')
#         pvaluebefore = self.request.query_params.getlist('pvaluebefore[]')
#         pval = self.request.query_params.get('pval', None)
#
#         #else:
#         if query.startswith("MR") or query.startswith("Pub"):
#             id = query
#         elif query.startswith("DGWAS"):
#             id_exposure = query
#             id_outcome = query
#         else:
#             exposure = query
#             outcome = query
#
#         # 根据查询参数构建查询条件
#         conditions = Q()
#         if id:
#             conditions &= Q(id=id)
#         # if exposureType:
#         #     conditions &= Q(exposureType=exposureType)
#         if exposure:
#             conditions &= Q(exposure__in=exposure)
#
#         if id_exposure:
#             conditions &= Q(id_exposure=id_exposure)
#         # if outcomeType:
#         #     conditions &= Q(outcomeType=outcomeType)
#         if outcome:
#             conditions &= Q(outcome=outcome)
#         if id_outcome:
#             conditions &= Q(id_outcome=id_outcome)
#         if analysistype:
#             conditions &= Q(analysistype__in=analysistype)
#         if pvaluebefore:
#             conditions &= Q(pvaluebefore__in=pvaluebefore)
#         if pval:
#             pval_float = float(pval)
#             conditions &= Q(pval__lt=pval_float)
#
#         # 如果有查询条件，则过滤查询集
#         if conditions:
#             queryset = queryset.filter(conditions)
#             # for item in queryset.values():
#             #     print(item)
#         return queryset
# home 添加或
# search 页面
class MrallViewSet(viewsets.ModelViewSet):
    serializer_class = MrallSerializer
    def get_queryset(self):
        queryset = Mrall.objects.all().order_by('id')
        print('------', queryset.count())
        query = self.request.query_params.get('query', None) #首页的查询
        id = self.request.query_params.get('id', None)
        exposure = self.request.query_params.getlist('exposure[]', None)
       # exposure = [s.replace('\ufeff', '') for s in exposure]
        id_exposure = self.request.query_params.get('id_exposure', None)
        id_exposure = id_exposure.replace('\ufeff', '')
        outcome = self.request.query_params.getlist('outcome[]', None)
        id_outcome = self.request.query_params.get('id_outcome', None)
        analysistype = self.request.query_params.getlist('analysistype[]')
        pvaluebefore = self.request.query_params.getlist('pvaluebefore[]')
        method = self.request.query_params.getlist('method[]')
        pval = self.request.query_params.get('pval', None)

        print('id:',  "id:", id,  "query:", query, "exposure:", exposure, "id_exposure:", id_exposure, "outcome:", outcome, "id_outcome:", id_outcome, "analysistype:", analysistype, "pvaluebefore:", pvaluebefore, "pval:", pval)
        if query:
            if query.startswith("MR") or query.startswith("Pub"):
                id = query
            elif query.startswith("DGWAS"):
                id_exposure = query
                id_outcome = query
                print('id_exposure:', id_exposure)
            elif query.startswith("ENSG"):
                id_exposure = query.replace('\ufeff', '')
            else:
                # exposure = [query]
                all_names1 = list(Disname.objects.values_list('name', flat=True))
                all_names3 = list(Genename.objects.values_list('symbol', flat=True))
                all_names3 = [name for name in all_names3 if name != 'NA']
                all_names4 = list(Proteinname.objects.values_list('name', flat=True))
                all_names = all_names1 + all_names3 + all_names4
                matched_names = fuzzy_match_strings2(query, all_names)
                exposure = matched_names

                if len(matched_names) > 5:
                    exposure = matched_names[:5]
                    # print('exposure:', exposure)

                matched_names_outcome = fuzzy_match_strings2(query, list(all_names1))
                outcome = matched_names_outcome
                if len(matched_names_outcome) > 5:
                    outcome = matched_names_outcome[:5]
                   # print('outcome:', outcome)
                #outcome = [query]

        conditions = Q()
        if exposure and outcome and exposure == outcome:
            # if exposure == outcome:
                conditions = Q(exposure__in=exposure) | Q(outcome__in=outcome)
        else:
            if exposure:
                print('here', exposure)
                conditions &= Q(exposure__in=exposure)
            if outcome:
                conditions &= Q(outcome__in=outcome)
        if id:
            conditions &= Q(id=id)
        # if exposure:
        #     conditions &= Q(exposure__in=exposure)
        if id_exposure:
            conditions &= Q(id_exposure=id_exposure)
        # if outcome:
        #     conditions &= Q(outcome__in=outcome)
        if id_outcome:
            conditions &= Q(id_outcome=id_outcome)
        if analysistype:
            conditions &= Q(analysistype__in=analysistype)
        if pvaluebefore:
            conditions &= Q(pvaluebefore__in=pvaluebefore)
        if method:
            conditions &= Q(method__in=method)
        if pval:
            pval_float = float(pval)
            conditions &= Q(pval__lt=pval_float)

        if conditions:
            queryset = queryset.filter(conditions)
            print("queryset:", queryset.count())
            # for item in queryset.values():
            #     print(item)
        return queryset

    def list(self, request, *args, **kwargs):

        page = self.request.query_params.get('page', 1)
        perPage_items = self.request.query_params.get('perPage_items', 10)

        try:
            page = int(page)
        except ValueError:
            page = 1
        try:
            perPage_items = int(perPage_items)
        except ValueError:
            perPage_items = 10

        query = self.get_queryset()
        paginator = Paginator(query, perPage_items)
        try:
            results = paginator.page(page)
        except EmptyPage:
            results = []

        serializer = self.get_serializer(results, many=True)
        # 使用序列化器序列化查询结果

        response_data = {
            'data': serializer.data,
            'total': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page
        }
        return Response(response_data)


# class MrallnewViewSet(viewsets.ModelViewSet):
#   #  serializer_class = MrallDisdisSerializer
#     def get_serializer_class(self):
#         sources = self.request.query_params.get('sources', None)
#         if sources == "A":
#             analysistype = self.request.query_params.get('analysistype', None)
#             print('analysistype:', analysistype)
#             if analysistype == 'disdis':
#                 return MrallDisdisSerializer
#             elif analysistype == 'pqtldis':
#                 return MrallPqtldisSerializer
#             elif analysistype == 'eqtldis':
#                 return MrallEqtldisSerializer
#             else:
#                 raise Http404("Invalid analysistype parameter")
#         else:
#             analysistype = self.request.query_params.get('analysistype', None)
#          #   print('analysistype:', analysistype)
#             if analysistype == 'disdis':
#                 return MrallDisdisSerializer
#             elif analysistype == 'pqtldis':
#                 return MrallPqtldisSerializer
#             elif analysistype == 'eqtldis':
#                 return MrallEqtldisSerializer
#             else:
#                 raise Http404("Invalid analysistype parameter")
#
#     def get_queryset(self):
#         sources = self.request.query_params.get('sources', None)
#         if sources == "A":
#             analysistype = self.request.query_params.get('analysistype', None)
#             if analysistype =='disdis':
#                 queryset = MrallDisdis.objects.all().order_by('id')
#             elif analysistype == 'pqtldis':
#                 queryset = MrallPqtldis.objects.all().order_by('id')
#             else:
#                 queryset = MrallEqtldis.objects.all().order_by('id')
#         else:
#             analysistype = self.request.query_params.get('analysistype', None)
#             if analysistype == 'disdis':
#                 queryset = MrallDisdis.objects.all().order_by('id')
#             elif analysistype == 'pqtldis':
#                 queryset = MrallPqtldis.objects.all().order_by('id')
#             else:
#                 queryset = MrallEqtldis.objects.all().order_by('id')
#
#
#       #  print('------', queryset.count())
#         query = self.request.query_params.get('query', None) #首页的查询
#         id = self.request.query_params.get('id', None)
#         id = id.replace('DMR', '')
#         exposure = self.request.query_params.getlist('exposure[]', None)
#        # exposure = [s.replace('\ufeff', '') for s in exposure]
#         id_exposure = self.request.query_params.get('id_exposure', None)
#         id_exposure = id_exposure.replace('\ufeff', '')
#         outcome = self.request.query_params.getlist('outcome[]', None)
#         id_outcome = self.request.query_params.get('id_outcome', None)
#         method = self.request.query_params.getlist('method[]')
#         pval = self.request.query_params.get('pval', None)
#
#       #  print('analysistype:', analysistype, "id:", id,  "query:", query, "exposure:", exposure, "id_exposure:", id_exposure, "outcome:", outcome, "id_outcome:", id_outcome,  "method:", method, "pval:", pval)
#         if query:
#             if query.startswith("MR") or query.startswith("Pub"):
#                 id = query
#             elif query.startswith("DGWAS"):
#                 id_exposure = query
#                 id_outcome = query
#                 print('id_exposure:', id_exposure)
#             elif query.startswith("ENSG"):
#                 id_exposure = query.replace('\ufeff', '')
#             else:
#                 # exposure = [query]
#                 all_names1 = list(Disname.objects.values_list('name', flat=True))
#                 all_names3 = list(Genename.objects.values_list('symbol', flat=True))
#                 all_names3 = [name for name in all_names3 if name != 'NA']
#                 all_names4 = list(Proteinname.objects.values_list('name', flat=True))
#                 all_names = all_names1 + all_names3 + all_names4
#                 matched_names = fuzzy_match_strings2(query, all_names)
#                 exposure = matched_names
#
#                 if len(matched_names) > 5:
#                     exposure = matched_names[:5]
#                     # print('exposure:', exposure)
#
#                 matched_names_outcome = fuzzy_match_strings2(query, list(all_names1))
#                 outcome = matched_names_outcome
#                 if len(matched_names_outcome) > 5:
#                     outcome = matched_names_outcome[:5]
#                    # print('outcome:', outcome)
#                 #outcome = [query]
#
#         conditions = Q()
#         if exposure and outcome and exposure == outcome:
#             # if exposure == outcome:
#                 conditions = Q(exposure__in=exposure) | Q(outcome__in=outcome)
#         else:
#             if exposure:
#               #  print('here', exposure)
#                 conditions &= Q(exposure__in=exposure)
#             if outcome:
#                 conditions &= Q(outcome__in=outcome)
#         if id:
#             conditions &= Q(id=id)
#         # if exposure:
#         #     conditions &= Q(exposure__in=exposure)
#         if id_exposure:
#             conditions &= Q(id_exposure=id_exposure)
#         # if outcome:
#         #     conditions &= Q(outcome__in=outcome)
#         if id_outcome:
#             conditions &= Q(id_outcome=id_outcome)
#         if method:
#            # print('method', method)
#             conditions &= Q(method__in=method)
#         if pval:
#             pval_float = float(pval)
#             conditions &= Q(pval__lt=pval_float)
#
#         if conditions:
#             queryset = queryset.filter(conditions)
#            # print("queryset:", queryset.count())
#             # for item in queryset.values():
#             #     print(item)
#         return queryset
#
#     def list(self, request, *args, **kwargs):
#
#         page = self.request.query_params.get('page', 1)
#         perPage_items = self.request.query_params.get('perPage_items', 9)
#
#         try:
#             page = int(page)
#         except ValueError:
#             page = 1
#         try:
#             perPage_items = int(perPage_items)
#         except ValueError:
#             perPage_items = 9
#
#         query = self.get_queryset()
#         # paginator = Paginator(query, perPage_items)
#         # try:
#         #     results = paginator.page(page)
#         # except EmptyPage:
#         #     results = []
#         #
#         # serializer = self.get_serializer(results, many=True)
#         # # 使用序列化器序列化查询结果
#         #
#         # response_data = {
#         #     'data': serializer.data,
#         #     'total': paginator.count,
#         #     'num_pages': paginator.num_pages,
#         #     'current_page': page
#         # }
#         # return Response(response_data)
#
#    # 检查查询集是否为空
#         if query.count() == 0:
#             # 如果为空，则返回空数据和元数据
#             return Response({
#                 'data': [],
#                 'total': 0,
#                 'num_pages': 0,
#                 'current_page': page
#             }, status=status.HTTP_200_OK)
#
#         paginator = Paginator(query, perPage_items)
#         try:
#             results = paginator.page(page)
#         except EmptyPage:
#             # 如果请求的页码超出范围，返回空数据和元数据
#             results = []
#             return Response({
#                 'data': [],
#                 'total': paginator.count,
#                 'num_pages': paginator.num_pages,
#                 'current_page': page
#             }, status=status.HTTP_200_OK)
#
#         serializer = self.get_serializer(results, many=True)
#
#         response_data = {
#             'data': serializer.data,
#             'total': paginator.count,
#             'num_pages': paginator.num_pages,
#             'current_page': page
#         }
#         return Response(response_data)

def flatten_and_unique(type_id_list):
    # 创建一个空集合用于存放所有数字
    unique_numbers = set()

    # 遍历 type_id_list 中的每个元素
    for item in type_id_list:
        # 如果元素是列表，则将列表中的每个数字添加到集合中
        if isinstance(item, list):
            unique_numbers.update(item)
        # 如果元素是单个数字，则直接添加到集合中
        elif isinstance(item, int):
            unique_numbers.add(item)

    # 将集合转换为列表并返回
    return list(unique_numbers)


def determine_type_ids(sources, analysistype, method):
    type_id_list = []
    # 定义映射关系，这里使用一个元组的列表来表示
    mapping = [
        ('A', 'disdis', 'all', [1, 2, 3, 4, 5, 6, 7]),
        ('A', 'pqtldis', 'all', [8, 9, 10, 11, 12, 13, 14]),
        ('A', 'pqtldis', 'all', [15, 16, 17, 18, 19, 20, 21]),
        ('A', 'disdis', 'Inverse variance weighted', [1]),
        ('A', 'disdis', 'Inverse variance weighted (fixed effects)', [2]),
        ('A', 'disdis', 'Inverse variance weighted (multiplicative random effects)', [3]),
        ('A', 'disdis', 'MR Egger', [4]),
        ('A', 'disdis', 'Simple median', [5]),
        ('A', 'disdis', 'Simple mode', [6]),
        ('A', 'disdis', 'Wald ratio', [7]),
        ('A', 'pqtldis', 'Inverse variance weighted', [8]),
        ('A', 'pqtldis', 'Inverse variance weighted (fixed effects)', [9]),
        ('A', 'pqtldis', 'Inverse variance weighted (multiplicative random effects)', [10]),
        ('A', 'pqtldis', 'MR Egger', [11]),
        ('A', 'pqtldis', 'Simple median', [12]),
        ('A', 'pqtldis', 'Simple mode', [13]),
        ('A', 'pqtldis', 'Wald ratio', [14]),
        ('A', 'eqtldis', 'Inverse variance weighted', [15]),
        ('A', 'eqtldis', 'Inverse variance weighted (fixed effects)', [16]),
        ('A', 'eqtldis', 'Inverse variance weighted (multiplicative random effects)', [17]),
        ('A', 'eqtldis', 'MR Egger', [18]),
        ('A', 'eqtldis', 'Simple median', [19]),
        ('A', 'eqtldis', 'Simple mode', [20]),
        ('A', 'eqtldis', 'Wald ratio', [21]),
        ('B', 'disdis', '', [22]),
        ('B', 'pqtldis', '', [23]),
        ('B', 'eqtldis', '', [24]),
        ('A', 'metdis', 'Inverse variance weighted', [27]),
        ('A', 'metdis', 'Inverse variance weighted (fixed effects)', [25]),
        ('A', 'metdis', 'Inverse variance weighted (multiplicative random effects)', [26]),
        ('A', 'metdis', 'MR Egger', [28]),
        ('A', 'metdis', 'Simple median', [29]),
        ('A', 'metdis', 'Simple mode', [30]),
        ('A', 'metdis', 'Wald ratio', [31]),
        ('A', 'mqtldis', 'Inverse variance weighted', [32]),
        ('A', 'mqtldis', 'Inverse variance weighted (fixed effects)', [33]),
        ('A', 'mqtldis', 'Inverse variance weighted (multiplicative random effects)', [34]),
        ('A', 'mqtldis', 'MR Egger', [35]),
        ('A', 'mqtldis', 'Simple median', [36]),
        ('A', 'mqtldis', 'Simple mode', [37]),
        ('A', 'mqtldis', 'Wald ratio', [38]),
    ]

    for src, atype, meth, tid in mapping:
        if src in sources and atype in analysistype and (meth == '' or meth in method):
            type_id_list.append(tid)
    type_id_list = flatten_and_unique(type_id_list)
    #print("type_id_list:", type_id_list)
    return type_id_list

# 2024 0903 可恢复
# class MrallnewViewSet(viewsets.ModelViewSet):
#     serializer_class = MrallCombineSerializer
#   #   def get_serializer_class(self):
#   #       sources = self.request.query_params.get('sources', None)
#   #       if sources == "A":
#   #           analysistype = self.request.query_params.get('analysistype', None)
#   #           print('analysistype:', analysistype)
#   #           if analysistype == 'disdis':
#   #               return MrallDisdisSerializer
#   #           elif analysistype == 'pqtldis':
#   #               return MrallPqtldisSerializer
#   #           elif analysistype == 'eqtldis':
#   #               return MrallEqtldisSerializer
#   #           else:
#   #               raise Http404("Invalid analysistype parameter")
#   #       else:
#   #           analysistype = self.request.query_params.get('analysistype', None)
#   #        #   print('analysistype:', analysistype)
#   #           if analysistype == 'disdis':
#   #               return MrallDisdisSerializer
#   #           elif analysistype == 'pqtldis':
#   #               return MrallPqtldisSerializer
#   #           elif analysistype == 'eqtldis':
#   #               return MrallEqtldisSerializer
#   #           else:
#   #               raise Http404("Invalid analysistype parameter")
#
#     def get_queryset(self):
#         # 获取传入的参数
#         sources = self.request.query_params.getlist('sources[]', None)
#         #print('sources:', sources)
#         analysistype = self.request.query_params.getlist('analysistype[]', None)
#         #print('analysistype', analysistype)
#         method = self.request.query_params.getlist('method[]', None)
#         #print('method:', method)
#         if not method:
#             method = ['all']
#         # 根据传入的参数确定对应的type_id
#        # type_id_list = []
#         type_id_list = determine_type_ids(sources, analysistype, method)
#        # print('type_id_list:', type_id_list)
#         queryset = MrallCombine.objects.all().order_by('ids')
#        # print('------', queryset.count())
#         query = self.request.query_params.get('query', None).strip() #首页的查询
#         disnames = self.request.query_params.get('disnames', None)
#         #print("disnames:", disnames)
#         ids = self.request.query_params.get('id', None)
#         #print('id_first:', id)
#
#         exposure = self.request.query_params.getlist('exposure[]', None)
#        # exposure = [s.replace('\ufeff', '') for s in exposure]
#         id_exposure = self.request.query_params.get('id_exposure', None).strip()
#         id_exposure = id_exposure.replace('\ufeff', '')
#         print('id_exposure:', id_exposure)
#         outcome = self.request.query_params.getlist('outcome[]', None)
#         id_outcome = self.request.query_params.get('id_outcome', None).strip()
#
#         pval = self.request.query_params.get('pval', None)
#         nsnp = self.request.query_params.get('nsnp', None)
#         or_field = self.request.query_params.get('or', None)
#
#         #print('analysistype:', analysistype, "id:", ids,  "query:", query, "exposure:", exposure, "id_exposure:", id_exposure, "outcome:", outcome, "id_outcome:", id_outcome,  "method:", method, "pval:", pval)
#         if query:
#             if query.startswith("DMR"): # or query.startswith("Pub"):
#                 ids = query
#             elif query.startswith("DGWAS"):
#                 id_exposure = query
#                 id_outcome = query
#               #  print('id_exposure:', id_exposure)
#             elif query.startswith("ENSG"):
#                 id_exposure = query.replace('\ufeff', '')
#             else:
#                 # exposure = [query]
#                 all_names1 = list(Disname.objects.values_list('name', flat=True))
#                 all_names3 = list(Genename.objects.values_list('symbol', flat=True))
#                 all_names3 = [name for name in all_names3 if name != 'NA']
#                 all_names4 = list(Proteinname.objects.values_list('name', flat=True))
#                 all_names = all_names1 + all_names3 + all_names4
#                 matched_names = fuzzy_match_strings2(query, all_names)
#                 exposure = matched_names
#
#                 if len(matched_names) > 5:
#                     exposure = matched_names[:5]
#                     # print('exposure:', exposure)
#
#                 matched_names_outcome = fuzzy_match_strings2(query, all_names1)
#                 outcome = matched_names_outcome
#                 if len(matched_names_outcome) > 5:
#                     outcome = matched_names_outcome[:5]
#                    # print('outcome:', outcome)
#                 #outcome = [query]
#
#         if disnames:
#             disnames_match = list(Disname.objects.values_list('name', flat=True))
#             outcome = fuzzy_match_strings2(disnames, disnames_match)
#             if len(outcome) > 5:
#                 outcome = outcome[:5]
#             exposure = outcome
#
#         conditions = Q()
#
#         if type_id_list:
#             conditions &= Q(type_id__in=type_id_list)
#
#         if pval:
#             pval_float = float(pval)
#            # print('pval', pval, type(pval))
#            # print('pval_float', pval, type(pval_float))
#             conditions &= Q(pval__lt=pval_float)
#
#         if nsnp:
#             nsnp = int(nsnp)
#             conditions &= Q(nsnp__gte=nsnp)
#
#         if or_field:
#             if or_field == '>1':
#                 conditions &= Q(or_field__gte=1)
#             else:
#                 conditions &= Q(or_field__lt=1)
#
#         if exposure and outcome and exposure == outcome:
#           #  print('here')
#             conditions &= Q(exposure__in=exposure) | Q(outcome__in=outcome)
#         else:
#             if exposure:
#               #  print('here', exposure)
#                 conditions &= Q(exposure__in=exposure)
#             if outcome:
#                 conditions &= Q(outcome__in=outcome)
#         if ids:
#            # print('ids:', ids)
#             ids = int(ids.strip().replace('DMR', ''))
#             conditions &= Q(ids=ids)
#         # if exposure:
#         #     conditions &= Q(exposure__in=exposure)
#
#         if id_exposure and id_outcome and id_exposure == id_outcome:
#             # if exposure == outcome:
#                 conditions &= Q(id_exposure=id_exposure) | Q(id_outcome=id_outcome)
#         else:
#             if id_exposure:
#               #  print('here')
#                 conditions &= Q(id_exposure=id_exposure)
#             # if outcome:
#             #     conditions &= Q(outcome__in=outcome)
#             if id_outcome:
#                 conditions &= Q(id_outcome=id_outcome)
#
#         if conditions:
#             queryset = queryset.filter(conditions)
#             print("queryset last:", queryset.count())
#             # for item in queryset.values():
#             #     print(item)
#         return queryset
#
#     def list(self, request, *args, **kwargs):
#
#         page = self.request.query_params.get('page', 1)
#         perPage_items = self.request.query_params.get('perPage_items', 10)
#
#         try:
#             page = int(page)
#         except ValueError:
#             page = 1
#         try:
#             perPage_items = int(perPage_items)
#         except ValueError:
#             perPage_items = 10
#
#         query = self.get_queryset()
#         # paginator = Paginator(query, perPage_items)
#         # try:
#         #     results = paginator.page(page)
#         # except EmptyPage:
#         #     results = []
#         #
#         # serializer = self.get_serializer(results, many=True)
#         # # 使用序列化器序列化查询结果
#         #
#         # response_data = {
#         #     'data': serializer.data,
#         #     'total': paginator.count,
#         #     'num_pages': paginator.num_pages,
#         #     'current_page': page
#         # }
#         # return Response(response_data)
#
#    # 检查查询集是否为空
#         if query.count() == 0:
#             # 如果为空，则返回空数据和元数据
#             return Response({
#                 'data': [],
#                 'total': 0,
#                 'num_pages': 0,
#                 'current_page': page
#             }, status=status.HTTP_200_OK)
#
#         paginator = Paginator(query, perPage_items)
#         try:
#             results = paginator.page(page)
#         except EmptyPage:
#             # 如果请求的页码超出范围，返回空数据和元数据
#             results = []
#             return Response({
#                 'data': [],
#                 'total': paginator.count,
#                 'num_pages': paginator.num_pages,
#                 'current_page': page
#             }, status=status.HTTP_200_OK)
#
#         serializer = self.get_serializer(results, many=True)
#
#         response_data = {
#             'data': serializer.data,
#             'total': paginator.count,
#             'num_pages': paginator.num_pages,
#             'current_page': page
#         }
#         return Response(response_data)

# 2024 10 17 带FDR矫正的返回值

class MrallnewViewSet(viewsets.ModelViewSet):
    serializer_class = MrallCombineSerializer
  #   def get_serializer_class(self):
  #       sources = self.request.query_params.get('sources', None)
  #       if sources == "A":
  #           analysistype = self.request.query_params.get('analysistype', None)
  #           print('analysistype:', analysistype)
  #           if analysistype == 'disdis':
  #               return MrallDisdisSerializer
  #           elif analysistype == 'pqtldis':
  #               return MrallPqtldisSerializer
  #           elif analysistype == 'eqtldis':
  #               return MrallEqtldisSerializer
  #           else:
  #               raise Http404("Invalid analysistype parameter")
  #       else:
  #           analysistype = self.request.query_params.get('analysistype', None)
  #        #   print('analysistype:', analysistype)
  #           if analysistype == 'disdis':
  #               return MrallDisdisSerializer
  #           elif analysistype == 'pqtldis':
  #               return MrallPqtldisSerializer
  #           elif analysistype == 'eqtldis':
  #               return MrallEqtldisSerializer
  #           else:
  #               raise Http404("Invalid analysistype parameter")

    def get_queryset(self):
        # 获取传入的参数
        sources = self.request.query_params.getlist('sources[]', None)
        #print('sources:', sources)
        analysistype = self.request.query_params.getlist('analysistype[]', None)
        #print('analysistype', analysistype)
        method = self.request.query_params.getlist('method[]', None)
        #print('method:', method)
        if not method:
            method = ['all']
        # 根据传入的参数确定对应的type_id
       # type_id_list = []
        type_id_list = determine_type_ids(sources, analysistype, method)
       # print('type_id_list:', type_id_list)
        queryset = MrallCombine.objects.all().order_by('ids')
       # print('------', queryset.count())
        query = self.request.query_params.get('query', None).strip() #首页的查询
        disnames = self.request.query_params.get('disnames', None)
        #print("disnames:", disnames)
        ids = self.request.query_params.get('id', None)
        #print('id_first:', id)

        exposure = self.request.query_params.getlist('exposure[]', None)
       # exposure = [s.replace('\ufeff', '') for s in exposure]
        id_exposure = self.request.query_params.get('id_exposure', None).strip()
        id_exposure = id_exposure.replace('\ufeff', '')
        print('id_exposure:', id_exposure)
        outcome = self.request.query_params.getlist('outcome[]', None)
        id_outcome = self.request.query_params.get('id_outcome', None).strip()

        pval = self.request.query_params.get('pval', None)
        nsnp = self.request.query_params.get('nsnp', None)
        or_field = self.request.query_params.get('or', None)

        #print('analysistype:', analysistype, "id:", ids,  "query:", query, "exposure:", exposure, "id_exposure:", id_exposure, "outcome:", outcome, "id_outcome:", id_outcome,  "method:", method, "pval:", pval)
        if query:
            if query.startswith("DMR"): # or query.startswith("Pub"):
                ids = query
            elif query.startswith("DGWAS"):
                id_exposure = query
                id_outcome = query
              #  print('id_exposure:', id_exposure)
            elif query.startswith("ENSG"):
                id_exposure = query.replace('\ufeff', '')
            elif query.startswith("cg"):
                id_exposure = query.replace('\ufeff', '')
            else:
                # exposure = [query]
                all_names1 = list(Disname.objects.values_list('name', flat=True))
                all_names3 = list(Genename.objects.values_list('symbol', flat=True))
                all_names3 = [name for name in all_names3 if name != 'NA']
                all_names4 = list(Proteinname.objects.values_list('name', flat=True))
                all_names5 = list(Metabolitename.objects.values_list('name', flat=True))
                all_names = all_names1 + all_names3 + all_names4 + all_names5
                matched_names = fuzzy_match_strings2(query, all_names)
                exposure = matched_names

                if len(matched_names) > 5:
                    exposure = matched_names[:5]
                    # print('exposure:', exposure)

                matched_names_outcome = fuzzy_match_strings2(query, all_names1)
                outcome = matched_names_outcome
                if len(matched_names_outcome) > 5:
                    outcome = matched_names_outcome[:5]
                   # print('outcome:', outcome)
                #outcome = [query]

        if disnames:
            disnames_match = list(Disname.objects.values_list('name', flat=True))
            outcome = fuzzy_match_strings2(disnames, disnames_match)
            if len(outcome) > 5:
                outcome = outcome[:5]
            exposure = outcome

        conditions = Q()

        if type_id_list:
            conditions &= Q(type_id__in=type_id_list)

        if pval:
            pval_float = float(pval)
           # print('pval', pval, type(pval))
           # print('pval_float', pval, type(pval_float))
            conditions &= Q(pval__lt=pval_float)

        if nsnp:
            nsnp = int(nsnp)
            conditions &= Q(nsnp__gte=nsnp)

        if or_field:
            if or_field == '>1':
                conditions &= Q(or_field__gte=1)
            else:
                conditions &= Q(or_field__lt=1)

        if exposure and outcome and exposure == outcome:
          #  print('here')
            conditions &= Q(exposure__in=exposure) | Q(outcome__in=outcome)
        else:
            if exposure:
              #  print('here', exposure)
                conditions &= Q(exposure__in=exposure)
            if outcome:
                conditions &= Q(outcome__in=outcome)
        if ids:
           # print('ids:', ids)
            ids = int(ids.strip().replace('DMR', ''))
            conditions &= Q(ids=ids)
        # if exposure:
        #     conditions &= Q(exposure__in=exposure)

        if id_exposure and id_outcome and id_exposure == id_outcome:
            # if exposure == outcome:
                conditions &= Q(id_exposure=id_exposure) | Q(id_outcome=id_outcome)
        else:
            if id_exposure:
              #  print('here')
                conditions &= Q(id_exposure=id_exposure)
            # if outcome:
            #     conditions &= Q(outcome__in=outcome)
            if id_outcome:
                conditions &= Q(id_outcome=id_outcome)

        if conditions:
            queryset = queryset.filter(conditions)
            print("queryset last:", queryset.count())
            # for item in queryset.values():
            #     print(item)
        return queryset

    def calculate_fdr(self, pvals):
        # 使用Benjamini-Hochberg方法计算FDR
        pvals_float = [float(pval) for pval in pvals]
        _, fdrs, _, _ = multipletests(pvals_float, method='fdr_bh')
        return fdrs

    def list(self, request, *args, **kwargs):

        page = self.request.query_params.get('page', 1)
        perPage_items = self.request.query_params.get('perPage_items', 10)

        try:
            page = int(page)
        except ValueError:
            page = 1
        try:
            perPage_items = int(perPage_items)
        except ValueError:
            perPage_items = 10

        query = self.get_queryset()

        # ----------------
        # 提取p值
        # pvals = list(query.values_list('pval', flat=True))
        # # 计算FDR
        # fdrs = self.calculate_fdr(pvals)
        #
        # # 将FDR值添加到查询集中
        # # for i, fdr in enumerate(fdrs):
        # #     query[i].fdr = fdr  # 假设你有一个fdr属性可以存储FDR值
        # # 将FDR值添加到查询集中
        # query_with_fdr = []
        # for item, fdr in zip(query, fdrs):
        #     item_data = model_to_dict(item)  # 将查询集中的每个对象转换为字典
        #     item_data['fdr'] = fdr  # 添加FDR值到字典中
        #     query_with_fdr.append(item_data)  # 将包含FDR值的字典添加到新的查询集
        # ----------------

   # 检查查询集是否为空
        if query.count() == 0:
            # 如果为空，则返回空数据和元数据
            return Response({
                'data': [],
                'total': 0,
                'num_pages': 0,
                'current_page': page
            }, status=status.HTTP_200_OK)

        # paginator = Paginator(query_with_fdr, perPage_items)

        paginator = Paginator(query, perPage_items)

        try:
            results = paginator.page(page)
        except EmptyPage:
            # 如果请求的页码超出范围，返回空数据和元数据
            results = []
            return Response({
                'data': [],
                'total': paginator.count,
                'num_pages': paginator.num_pages,
                'current_page': page
            }, status=status.HTTP_200_OK)

        serializer = self.get_serializer(results, many=True)
       # print('serializerdata:', serializer.data)
        response_data = {
            'data': serializer.data,
            'total': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page
        }
        return Response(response_data)


# class CompositeSerializer(serializers.Serializer):
#     def to_representation(self, instance):
#         if isinstance(instance, MrallDisdis):
#             return MrallDisdisSerializer(instance).data
#         elif isinstance(instance, MrallPqtldis):
#             return MrallPqtldisSerializer(instance).data
#         elif isinstance(instance, MrallEqtldis):
#             return MrallEqtldisSerializer(instance).data
#         else:
#             raise Exception("Unexpected type of tagged object")


# class MrallnewViewSet(viewsets.ModelViewSet):
#     #serializer_class = MrallDisdisSerializer
#     # serializer_class = UniversalMrallSerializer
#
#     def get_serializer_class(self):
#         analysistype = self.request.query_params.getlist('analysistype[]', None)
#         print('analysistypes:', analysistype)
#
#         serializer_mapping = {
#             'disdis': MrallDisdisSerializer,
#             'pqtldis': MrallPqtldisSerializer,
#             'eqtldis': MrallEqtldisSerializer
#         }
#
#         if not analysistype:
#             raise Http404("Invalid or missing analysistype parameter")
#
#         serializers = [serializer_mapping.get(at) for at in analysistype if at in serializer_mapping]
#
#         if not serializers:
#             raise Http404("Invalid analysistype parameter")
#
#         if len(serializers) == 1:
#             return serializers[0]
#
#         return CompositeSerializer(serializers)
#
#
#     def get_queryset(self):
#         analysistype = self.request.query_params.getlist('analysistype[]', None)
#         queryset = None
#
#         if 'disdis' in analysistype:
#             queryset_disdis = MrallDisdis.objects.all().order_by('id')
#             queryset = queryset_disdis if queryset is None else queryset.union(queryset_disdis)
#
#         if 'pqtldis' in analysistype:
#             queryset_pqtldis = MrallPqtldis.objects.all().order_by('id')
#             queryset = queryset_pqtldis if queryset is None else queryset.union(queryset_pqtldis)
#
#         if 'eqtldis' in analysistype:
#             queryset_eqtldis = MrallEqtldis.objects.all().order_by('id')
#             queryset = queryset_eqtldis if queryset is None else queryset.union(queryset_eqtldis)
#
#         if queryset is None:
#             raise Http404("Invalid analysistype parameter")
#
#         print('------', queryset.count())
#         query = self.request.query_params.get('query', None) #首页的查询
#         id = self.request.query_params.get('id', None)
#         exposure = self.request.query_params.getlist('exposure[]', None)
#        # exposure = [s.replace('\ufeff', '') for s in exposure]
#         id_exposure = self.request.query_params.get('id_exposure', None)
#         id_exposure = id_exposure.replace('\ufeff', '')
#         outcome = self.request.query_params.getlist('outcome[]', None)
#         id_outcome = self.request.query_params.get('id_outcome', None)
#         method = self.request.query_params.getlist('method[]')
#         pval = self.request.query_params.get('pval', None)
#
#         print('analysistype:', analysistype, "id:", id,  "query:", query, "exposure:", exposure, "id_exposure:", id_exposure, "outcome:", outcome, "id_outcome:", id_outcome,  "method:", method, "pval:", pval)
#         if query:
#             if query.startswith("MR") or query.startswith("Pub"):
#                 id = query
#             elif query.startswith("DGWAS"):
#                 id_exposure = query
#                 id_outcome = query
#                 print('id_exposure:', id_exposure)
#             elif query.startswith("ENSG"):
#                 id_exposure = query.replace('\ufeff', '')
#             else:
#                 # exposure = [query]
#                 all_names1 = list(Disname.objects.values_list('name', flat=True))
#                 all_names3 = list(Genename.objects.values_list('symbol', flat=True))
#                 all_names3 = [name for name in all_names3 if name != 'NA']
#                 all_names4 = list(Proteinname.objects.values_list('name', flat=True))
#                 all_names = all_names1 + all_names3 + all_names4
#                 matched_names = fuzzy_match_strings2(query, all_names)
#                 exposure = matched_names
#
#                 if len(matched_names) > 5:
#                     exposure = matched_names[:5]
#                     # print('exposure:', exposure)
#
#                 matched_names_outcome = fuzzy_match_strings2(query, list(all_names1))
#                 outcome = matched_names_outcome
#                 if len(matched_names_outcome) > 5:
#                     outcome = matched_names_outcome[:5]
#                    # print('outcome:', outcome)
#                 #outcome = [query]
#
#         conditions = Q()
#         if exposure and outcome and exposure == outcome:
#             # if exposure == outcome:
#                 conditions = Q(exposure__in=exposure) | Q(outcome__in=outcome)
#         else:
#             if exposure:
#                 print('here', exposure)
#                 conditions &= Q(exposure__in=exposure)
#             if outcome:
#                 conditions &= Q(outcome__in=outcome)
#         if id:
#             conditions &= Q(id=id)
#         # if exposure:
#         #     conditions &= Q(exposure__in=exposure)
#         if id_exposure:
#             conditions &= Q(id_exposure=id_exposure)
#         # if outcome:
#         #     conditions &= Q(outcome__in=outcome)
#         if id_outcome:
#             conditions &= Q(id_outcome=id_outcome)
#         if method:
#             print('method', method)
#             conditions &= Q(method__in=method)
#         if pval:
#             pval_float = float(pval)
#             conditions &= Q(pval__lt=pval_float)
#
#         if conditions:
#             queryset = queryset.filter(conditions)
#             print("queryset:", queryset.count())
#             # for item in queryset.values():
#             #     print(item)
#         return queryset
#
#     def list(self, request, *args, **kwargs):
#
#         page = self.request.query_params.get('page', 1)
#         perPage_items = self.request.query_params.get('perPage_items', 9)
#
#         try:
#             page = int(page)
#         except ValueError:
#             page = 1
#         try:
#             perPage_items = int(perPage_items)
#         except ValueError:
#             perPage_items = 9
#
#         query = self.get_queryset()
#         paginator = Paginator(query, perPage_items)
#         try:
#             results = paginator.page(page)
#         except EmptyPage:
#             results = []
#
#         serializer = self.get_serializer(results, many=True)
#         # 使用序列化器序列化查询结果
#
#         response_data = {
#             'data': serializer.data,
#             'total': paginator.count,
#             'num_pages': paginator.num_pages,
#             'current_page': page
#         }
#         return Response(response_data)

class DisnameViewSet(viewsets.ModelViewSet):
    queryset = Disname.objects.all()
    serializer_class = DisnameSerializer


class BrowsedisnameViewSet(viewsets.ModelViewSet):
    serializer_class = DisnameSerializer

    def get_queryset(self):
        query = self.request.query_params.get('browseDisname', None)
        #print('browseDisname', query)
        if query:
            query = query.strip()
            if query:
                all_names = list(Disname.objects.values_list('name', flat=True))
                matched_names = fuzzy_match_strings3(query, all_names)
                #print(matched_names[0:50])
                preserved = Case(*[When(name=name, then=Value(index)) for index, (name, _) in enumerate(matched_names)])
               # return Disname.objects.filter(name__in=matched_names)
                return Disname.objects.filter(name__in=[name for name, _ in matched_names]).order_by(preserved)

        return Disname.objects.all().order_by('name')

    def list(self, request, *args, **kwargs):
        page = self.request.query_params.get('page', 1)
        per_page_items = self.request.query_params.get('perPage_items', 10)

        try:
            page = int(page)
        except ValueError:
            page = 1
        try:
            per_page_items = int(per_page_items)
        except ValueError:
            per_page_items = 10

        query = self.get_queryset()

        # 检查查询集是否为空
        if query.count() == 0:
            # 如果为空，则返回空数据和元数据
            return Response({
                'data': [],
                'total': 0,
                'num_pages': 0,
                'current_page': page
            }, status=status.HTTP_200_OK)

        paginator = Paginator(query, per_page_items)
        try:
            results = paginator.page(page)
        except EmptyPage:
            # 如果请求的页码超出范围，返回空数据和元数据
            return Response({
                'data': [],
                'total': paginator.count,
                'num_pages': paginator.num_pages,
                'current_page': page
            }, status=status.HTTP_200_OK)

        serializer = self.get_serializer(results, many=True)
        #print('serializer.data:', serializer.data)
        response_data = {
            'data': serializer.data,
            'total': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page
        }
        return Response(response_data)


class BrowsegenenameViewSet(viewsets.ModelViewSet):
    serializer_class = GenenameSerializer

    def get_queryset(self):
        query = self.request.query_params.get('browseGenename', None)
        print('browseGenename:', query)
        if query:
            query = query.strip()
            if query:
                if query.startswith("ENSG"):
                    #query = query.replace('\ufeff', '')
                    all_names = list(Genename.objects.values_list('ensg', flat=True))
                    matched_names = fuzzy_match_strings4(query, all_names)
                    #print(matched_names[0:50])
                    preserved = Case(*[When(ensg=name, then=Value(index)) for index, (name, _) in enumerate(matched_names)])
                   # return Disname.objects.filter(name__in=matched_names)
                    return Genename.objects.filter(ensg__in=[name for name, _ in matched_names]).order_by(preserved)
                else:
                    all_names = list(Genename.objects.values_list('symbol', flat=True))
                    matched_names = fuzzy_match_strings4(query, all_names)
                    # print(matched_names[0:50])
                    preserved = Case(*[When(symbol=name, then=Value(index)) for index, (name, _) in enumerate(matched_names)])
                    # return Disname.objects.filter(name__in=matched_names)
                    return Genename.objects.filter(symbol__in=[name for name, _ in matched_names]).order_by(preserved)

        return Genename.objects.all().order_by('symbol')

    def list(self, request, *args, **kwargs):
        page = self.request.query_params.get('page', 1)
        per_page_items = self.request.query_params.get('perPage_items', 10)

        try:
            page = int(page)
        except ValueError:
            page = 1
        try:
            per_page_items = int(per_page_items)
        except ValueError:
            per_page_items = 10

        query = self.get_queryset()

        # 检查查询集是否为空
        if query.count() == 0:
            #print('yes')
            # 如果为空，则返回空数据和元数据
            return Response({
                'data': [],
                'total': 0,
                'num_pages': 0,
                'current_page': page
            }, status=status.HTTP_200_OK)

        paginator = Paginator(query, per_page_items)
        try:
            results = paginator.page(page)
        except EmptyPage:
            # 如果请求的页码超出范围，返回空数据和元数据
            return Response({
                'data': [],
                'total': paginator.count,
                'num_pages': paginator.num_pages,
                'current_page': page
            }, status=status.HTTP_200_OK)

        serializer = self.get_serializer(results, many=True)
        #print('serializer.data:', serializer.data)
        response_data = {
            'data': serializer.data,
            'total': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page
        }
        return Response(response_data)


class BrowsepronameViewSet(viewsets.ModelViewSet):
    serializer_class = ProteinnameSerializer

    def get_queryset(self):
        query = self.request.query_params.get('browseProname', None)
       # print('browseProname:', query)
        if query:
            query = query.strip()
            if query:
                all_names = list(Proteinname.objects.values_list('name', flat=True))
                matched_names = fuzzy_match_strings3(query, all_names)
                #print(matched_names[0:50])
                preserved = Case(*[When(name=name, then=Value(index)) for index, (name, _) in enumerate(matched_names)])
               # return Disname.objects.filter(name__in=matched_names)
                return Proteinname.objects.filter(name__in=[name for name, _ in matched_names]).order_by(preserved)

        return Proteinname.objects.all().order_by('name')

    def list(self, request, *args, **kwargs):
        page = self.request.query_params.get('page', 1)
        per_page_items = self.request.query_params.get('perPage_items', 10)

        try:
            page = int(page)
        except ValueError:
            page = 1
        try:
            per_page_items = int(per_page_items)
        except ValueError:
            per_page_items = 10

        query = self.get_queryset()

        # 检查查询集是否为空
        if query.count() == 0:
            #print('yes')
            # 如果为空，则返回空数据和元数据
            return Response({
                'data': [],
                'total': 0,
                'num_pages': 0,
                'current_page': page
            }, status=status.HTTP_200_OK)

        paginator = Paginator(query, per_page_items)
        try:
            results = paginator.page(page)
        except EmptyPage:
            # 如果请求的页码超出范围，返回空数据和元数据
            return Response({
                'data': [],
                'total': paginator.count,
                'num_pages': paginator.num_pages,
                'current_page': page
            }, status=status.HTTP_200_OK)

        serializer = self.get_serializer(results, many=True)
        #print('serializer.data:', serializer.data)
        response_data = {
            'data': serializer.data,
            'total': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page
        }
        return Response(response_data)


class BrowseMetnameViewSet(viewsets.ModelViewSet):
    serializer_class = MetabolitenameSerializer

    def get_queryset(self):
        query = self.request.query_params.get('browseMetname', None)
       # print('browseProname:', query)
        if query:
            query = query.strip()
            if query:
                all_names = list(Metabolitename.objects.values_list('name', flat=True))
                matched_names = fuzzy_match_strings3(query, all_names)
                #print(matched_names[0:50])
                preserved = Case(*[When(name=name, then=Value(index)) for index, (name, _) in enumerate(matched_names)])
               # return Disname.objects.filter(name__in=matched_names)
                return Metabolitename.objects.filter(name__in=[name for name, _ in matched_names]).order_by(preserved)

        return Metabolitename.objects.all().order_by('name')

    def list(self, request, *args, **kwargs):
        page = self.request.query_params.get('page', 1)
        per_page_items = self.request.query_params.get('perPage_items', 10)

        try:
            page = int(page)
        except ValueError:
            page = 1
        try:
            per_page_items = int(per_page_items)
        except ValueError:
            per_page_items = 10

        query = self.get_queryset()

        # 检查查询集是否为空
        if query.count() == 0:
            #print('yes')
            # 如果为空，则返回空数据和元数据
            return Response({
                'data': [],
                'total': 0,
                'num_pages': 0,
                'current_page': page
            }, status=status.HTTP_200_OK)

        paginator = Paginator(query, per_page_items)
        try:
            results = paginator.page(page)
        except EmptyPage:
            # 如果请求的页码超出范围，返回空数据和元数据
            return Response({
                'data': [],
                'total': paginator.count,
                'num_pages': paginator.num_pages,
                'current_page': page
            }, status=status.HTTP_200_OK)

        serializer = self.get_serializer(results, many=True)
        #print('serializer.data:', serializer.data)
        response_data = {
            'data': serializer.data,
            'total': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page
        }
        return Response(response_data)


class BrowsecpgsiteViewSet(viewsets.ModelViewSet):
    serializer_class = CpgsitenameSerializer

    def get_queryset(self):
        query = self.request.query_params.get('browseCpgname', None)
       # print('browseProname:', query)
        if query:
            query = query.strip()
            if query:
                all_names = list(Cpgsitename.objects.values_list('name', flat=True))
                matched_names = fuzzy_match_strings3(query, all_names)
                #print(matched_names[0:50])
                preserved = Case(*[When(name=name, then=Value(index)) for index, (name, _) in enumerate(matched_names)])
               # return Disname.objects.filter(name__in=matched_names)
                return Cpgsitename.objects.filter(name__in=[name for name, _ in matched_names]).order_by(preserved)

        return Cpgsitename.objects.all().order_by('name')

    def list(self, request, *args, **kwargs):
        page = self.request.query_params.get('page', 1)
        per_page_items = self.request.query_params.get('perPage_items', 10)

        try:
            page = int(page)
        except ValueError:
            page = 1
        try:
            per_page_items = int(per_page_items)
        except ValueError:
            per_page_items = 10

        query = self.get_queryset()

        # 检查查询集是否为空
        if query.count() == 0:
            #print('yes')
            # 如果为空，则返回空数据和元数据
            return Response({
                'data': [],
                'total': 0,
                'num_pages': 0,
                'current_page': page
            }, status=status.HTTP_200_OK)

        paginator = Paginator(query, per_page_items)
        try:
            results = paginator.page(page)
        except EmptyPage:
            # 如果请求的页码超出范围，返回空数据和元数据
            return Response({
                'data': [],
                'total': paginator.count,
                'num_pages': paginator.num_pages,
                'current_page': page
            }, status=status.HTTP_200_OK)

        serializer = self.get_serializer(results, many=True)
        #print('serializer.data:', serializer.data)
        response_data = {
            'data': serializer.data,
            'total': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page
        }
        return Response(response_data)

# class DisnameSimilarityView(viewsets.ModelViewSet):
#     serializer_class = DisnameSerializer
#     def get_queryset(self):
#         # 返回一个空的查询集
#         return Disname.objects.none()
#
#     def get(self, request, *args, **kwargs):
#         names_string = request.GET.get('names')
#         print('qianduan:', names_string)
#         if names_string:
#
#             names_list = names_string
#             # names_list = names_string.split(',')  # 将字符串分割成列表
#             all_names = Disname.objects.values_list('name', flat=True)
#             matched_names, _ = fuzzy_match_strings(names_list, list(all_names))
#             top_matched_names = matched_names[:20]  # 取最相似的前20个名字
#             return JsonResponse(top_matched_names, safe=False)
#         else:
#             return JsonResponse({"error": "No names provided"}, status=400)
#
# 暴露名称


def get_names_by_type(type_list):
    # 使用Q对象构建查询条件
    query = Q()
    for type_value in type_list:
        query |= Q(type=type_value)
    #print('Constructed query:', query)
    # 查询数据库
    results = AllexposureName.objects.filter(query).values_list('name', flat=True)

    # 将查询结果转换为列表并返回
    return list(results)


def get_names_by_type2(type_list):
    # 使用Q对象构建查询条件
    query = Q()
    for type_value in type_list:
        query |= Q(type=type_value)
    #print('Constructed query:', query)
    # 查询数据库
    results = AlloutcomeName.objects.filter(query).values_list('name', flat=True)

    # 将查询结果转换为列表并返回
    return list(results)


class AllexposurenameSimilarityView(View):
    # def get_queryset(self):
    #     # 返回一个空的查询集
    #     return Disname.objects.none()
    def get(self, request, *args, **kwargs):
        sources = request.GET.getlist('sources[]', [])
        analysistype = request.GET.getlist('analysistype[]', [])
        method = request.GET.getlist('method[]', [])
        names_string = request.GET.get('names', '')

        # sources = ['A']
        # analysistype = ['disdis']
        # method = ['']
        if not method:
            method = ['all']
        type_id_list = determine_type_ids(sources, analysistype, method)
        results = get_names_by_type(type_id_list)
        if names_string:
            names_list = names_string.split(',')
            matched_names = fuzzy_match_strings(names_string, results)
            top_matched_names = matched_names[:50]
            return JsonResponse(top_matched_names, safe=False)
        else:
            return JsonResponse({"error": "No names provided"}, status=400)


class AlloutcomenameSimilarityView(View):
    # def get_queryset(self):
    #     # 返回一个空的查询集
    #     return Disname.objects.none()
    def get(self, request, *args, **kwargs):
        sources = request.GET.getlist('sources[]', [])
        analysistype = request.GET.getlist('analysistype[]', [])
        method = request.GET.getlist('method[]', [])
        names_string = request.GET.get('names', '')

        # sources = ['A']
        # analysistype = ['disdis']
        # method = ['']
        if not method:
            method = ['all']
        type_id_list = determine_type_ids(sources, analysistype, method)
        print("type_id_list:", type_id_list)
        results = get_names_by_type2(type_id_list)
        if names_string:
          #  names_list = names_string.split(',')
            matched_names = fuzzy_match_strings(names_string, results)
            top_matched_names = matched_names[:50]
            #print("top_matched_names:",top_matched_names )
            return JsonResponse(top_matched_names, safe=False)
        else:
            return JsonResponse({"error": "No names provided"}, status=400)

# class AllexposurenameSimilarityView(View):
#     def get_queryset(self):
#         # 返回一个空的查询集
#         sources = self.request.query_params.getlist('sources[]', None)
#         analysistype = self.request.query_params.getlist('analysistype[]', None)
#         method = self.request.query_params.getlist('method[]', None)
#         names_string = self.request.query_params.get('names', None)
#         if not method:
#             method = ['all']
#         type_id_list = determine_type_ids(sources, analysistype, method)
#         print(type_id_list)
#         results = get_names_by_type(type_id_list)
#         if names_string:
#             names_list = names_string.split(',')  # 将字符串分割成列表
#             matched_names = fuzzy_match_strings(names_list, results)
#             top_matched_names = matched_names[:50]  # 取最相似的前20个名字
#             # print(top_matched_names)
#             return JsonResponse(top_matched_names, safe=False)
#         else:
#             return JsonResponse({"error": "No names provided"}, status=400)


class DisnameSimilarityView(View):
    def get_queryset(self):
        # 返回一个空的查询集
        return Disname.objects.none()

    def get(self, request, *args, **kwargs):
        names_string = request.GET.get('names')
        if names_string:
            names_list = names_string.split(',')  # 将字符串分割成列表
            all_names = Disname.objects.values_list('name', flat=True)
            #top_20_matches = fuzzy_match_string(A[0], B)
            matched_names = fuzzy_match_strings(names_list, list(all_names))
            top_matched_names = matched_names[:50]  # 取最相似的前20个名字
            # print(top_matched_names)
            return JsonResponse(top_matched_names, safe=False)
        else:
            return JsonResponse({"error": "No names provided"}, status=400)


class DisnameSimilarityViewOutcome(View):
    def get_queryset(self):
        # 返回一个空的查询集
        return DisnameOutcome.objects.none()

    def get(self, request, *args, **kwargs):
        names_string = request.GET.get('names')
        if names_string:
            names_list = names_string
            # names_list = names_string.split(',')  # 将字符串分割成列表
            all_names = DisnameOutcome.objects.values_list('name', flat=True)
            #top_20_matches = fuzzy_match_string(A[0], B)
            matched_names = fuzzy_match_strings(names_list, list(all_names))
            top_matched_names = matched_names[:50]  # 取最相似的前20个名字
            print('DisnameSimilarityViewOutcome:', top_matched_names)
            return JsonResponse(top_matched_names, safe=False)
        else:
            return JsonResponse({"error": "No names provided"}, status=400)


# class GenenameSimilarityView(View):
#     def get_queryset(self):
#         # 返回一个空的查询集
#         return Genename.objects.none()
#
#     def get(self, request, *args, **kwargs):
#         names_string = request.GET.get('names')
#         if names_string:
#             names_list = names_string
#             # names_list = names_string.split(',')  # 将字符串分割成列表
#             all_names = Genename.objects.values_list('symbol', flat=True)
#             #top_20_matches = fuzzy_match_string(A[0], B)
#             matched_names = fuzzy_match_strings(names_list, list(all_names))
#             top_matched_names = matched_names[:50]  # 取最相似的前20个名字
#             print(top_matched_names)
#             return JsonResponse(top_matched_names, safe=False)
#         else:
#             return JsonResponse({"error": "No names provided"}, status=400)
#

class GenenameSimilarityView(View):
    def get_queryset(self):
        # 返回一个空的查询集
        return Genename.objects.none()

    def get(self, request, *args, **kwargs):
        names_string = request.GET.get('names')
        print('names_string:', names_string)
        if names_string:
            names_list = names_string
            # names_list = names_string.split(',')  # 将字符串分割成列表

            # if "ENSG" in names_string:
            #     all_names = Genename.objects.values_list('ensg', flat=True)
           # else:
            all_names = Genename.objects.values_list('symbol', flat=True)
            # Remove 'NA' values
            all_names = [name for name in all_names if name != 'NA']

            matched_names = fuzzy_match_strings(names_list, list(all_names))
            top_matched_names = matched_names[:50]  # 取最相似的前50个名字
           #  print(top_matched_names)
            return JsonResponse(top_matched_names, safe=False)
        else:
            return JsonResponse({"error": "No names provided"}, status=400)


class GenenameSimilarityViewOutcome(View):
    def get_queryset(self):
        # 返回一个空的查询集
        return GenenameOutcome.objects.none()

    def get(self, request, *args, **kwargs):
        names_string = request.GET.get('names')
        if names_string:
            names_list = names_string
            # names_list = names_string.split(',')  # 将字符串分割成列表
            all_names = GenenameOutcome.objects.values_list('name', flat=True)
            #top_20_matches = fuzzy_match_string(A[0], B)
            matched_names = fuzzy_match_strings(names_list, list(all_names))
            top_matched_names = matched_names[:50]  # 取最相似的前20个名字
            print(top_matched_names)
            return JsonResponse(top_matched_names, safe=False)
        else:
            return JsonResponse({"error": "No names provided"}, status=400)


class ProteinnameSimilarityView(View):
    def get_queryset(self):
        # 返回一个空的查询集
        return Proteinname.objects.none()

    def get(self, request, *args, **kwargs):
        names_string = request.GET.get('names')
        if names_string:
            names_list = names_string
            # names_list = names_string.split(',')  # 将字符串分割成列表

            all_names = Proteinname.objects.values_list('name', flat=True)

            matched_names = fuzzy_match_strings(names_list, list(all_names))
            top_matched_names = matched_names[:50]  # 取最相似的前50个名字
            # print(top_matched_names)
            return JsonResponse(top_matched_names, safe=False)
        else:
            return JsonResponse({"error": "No names provided"}, status=400)


class ProteinnameSimilarityViewOutcome(View):
    def get_queryset(self):
        # 返回一个空的查询集
        return ProteinnameOutcome.objects.none()

    def get(self, request, *args, **kwargs):
        names_string = request.GET.get('names')
        if names_string:
            names_list = names_string
            # names_list = names_string.split(',')  # 将字符串分割成列表
            all_names = ProteinnameOutcome.objects.values_list('name', flat=True)
            #top_20_matches = fuzzy_match_string(A[0], B)
            matched_names = fuzzy_match_strings(names_list, list(all_names))
            top_matched_names = matched_names[:50]  # 取最相似的前20个名字
            # print(top_matched_names)
            return JsonResponse(top_matched_names, safe=False)
        else:
            return JsonResponse({"error": "No names provided"}, status=400)


class GWASdatasetViewSet(viewsets.ModelViewSet):
    serializer_class = GwasSerializer
    def get_queryset(self):
        queryset = Gwas.objects.all().order_by('gwasid')
        #print("queryset:", queryset.count())
        query = self.request.query_params.get('query', None)
        #print('query:', query)
        trait = None
        gwasid = None
        if query:
            if query.startswith("DGWAS"):
                #print('here1')
                gwasid = query

            else:
                all_names = list(Gwas.objects.values_list('phenotype', flat=True))
                matched_names = fuzzy_match_strings(query, all_names)
                if len(matched_names) > 20:
                    trait = matched_names[:20]
                else:
                    trait = matched_names

        conditions = Q()
        if gwasid:
            #print('gwasid here', gwasid)
            conditions &= Q(gwasid=gwasid)
        # print('trait:', trait)
        if trait:
            conditions &= Q(phenotype__in=trait)

        if conditions:
            queryset = queryset.filter(conditions)
            #print("queryset.count():", queryset.count())
            # for item in queryset.values():
            #     print(item)
        return queryset

    def list(self, request, *args, **kwargs):

        page = self.request.query_params.get('page', 1)
        perPage_items = self.request.query_params.get('perPage_items', 20)

        try:
            page = int(page)
        except ValueError:
            page = 1
        try:
            perPage_items = int(perPage_items)
        except ValueError:
            perPage_items = 20

        query = self.get_queryset()
        paginator = Paginator(query, perPage_items)
        try:
            results = paginator.page(page)
        except EmptyPage:
            results = []

        serializer = self.get_serializer(results, many=True)
        # 使用序列化器序列化查询结果

        response_data = {
            'data': serializer.data,
            'total': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page
        }
        return Response(response_data)


def download_gwasData(request):
    # 从请求参数中获取ID
    file_id = request.GET.get('params').strip().strip('\ufeff')
    print('ID:', file_id)
    # 拼接文件路径
    file_path = os.path.join('/mnt/zx/DMRdb/GWAS_all/', f'{file_id}.gz')
    print('filepath:', file_path)
    # 检查文件是否存在
    if not os.path.exists(file_path):
        print('no')
        raise Http404('File not found')

    # 读取文件内容并创建HTTP响应
    with open(file_path, 'rb') as f:
        response = HttpResponse(f.read(), content_type='application/zip')
        response['Content-Disposition'] = f'attachment; filename="{file_id}.gz"'
        return response


class DownloadpagesingleViewSet(viewsets.ModelViewSet):
    serializer_class = DownpagesingleSerializer
    def get_queryset(self):
        queryset = DownloadpageSingle.objects.all().order_by('index')
        #print("queryset:", queryset.count())
        aimedType = self.request.query_params.get('aimedType', None)
        category = self.request.query_params.get('category', None)
        traitName = self.request.query_params.get('traitName', None)
        select = self.request.query_params.get('select', None)
        type = None
        name = None
        print('aimedType:', aimedType, 'categoery:', category, "traitName :", traitName, "select :", select)
        if aimedType == 'outcome':
            if category == 'Disease(GWAS)':
                type = 0
            elif category == 'Gene(eQTL)':
                type = 1
            elif category == 'Protein(pQTL)':
                type = 2
            elif category == 'Metabolite(GWAS)':
                type = 3
            else:
                type = 4
        else:
            if category == 'Disease(GWAS)':
                type = 5
            elif category == 'Gene(eQTL)':
                type = 6
            elif category == 'Protein(pQTL)':
                type = 7
            elif category == 'Metabolite(GWAS)':
                type = 8
            else:
                type = 9


        if traitName:
            if select == 'name':
                all_names = list(Gwas.objects.values_list('phenotype', flat=True))
                matched_names = fuzzy_match_strings(traitName, all_names)
                if len(matched_names) > 20:
                    traitName = matched_names[:20]
                else:
                    traitName = matched_names

        conditions = Q()
        if type:
            print('type:', type)
            conditions &= Q(type=type)
        # print('trait:', trait)
        if traitName:
            if select == 'name':
                conditions &= Q(name__in=traitName)
            else:
                conditions &= Q(id__in=traitName)


        if conditions:
            queryset = queryset.filter(conditions)
            print("queryset.count():", queryset.count())
            # for item in queryset.values():
            #     print(item)
        return queryset

    def list(self, request, *args, **kwargs):

        page = self.request.query_params.get('page', 1)
        perPage_items = self.request.query_params.get('perPage_items', 20)

        try:
            page = int(page)
        except ValueError:
            page = 1
        try:
            perPage_items = int(perPage_items)
        except ValueError:
            perPage_items = 20

        query = self.get_queryset()
        paginator = Paginator(query, perPage_items)
        try:
            results = paginator.page(page)
        except EmptyPage:
            results = []

        serializer = self.get_serializer(results, many=True)
        # 使用序列化器序列化查询结果

        response_data = {
            'data': serializer.data,
            'total': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page
        }
        return Response(response_data)


class PhewasViewSet(viewsets.ModelViewSet):
    serializer_class = PhewasSerializer

    def get_queryset(self):
        queryset = Phewas.objects.all().order_by('id')
        rsid = self.request.query_params.get('query', None)
        print('rsid:', rsid)
        if rsid:
            queryset = queryset.filter(Q(snp=rsid))
        return queryset

    def list(self, request, *args, **kwargs):
        page = request.query_params.get('page', 1)
        perPage_items = request.query_params.get('perPage_items', 10)
        try:
            page = int(page)
        except ValueError:
            page = 1
        try:
            perPage_items = int(perPage_items)
        except ValueError:
            page = 10

        queryset = self.get_queryset()
        paginator = Paginator(queryset, perPage_items)
        try:
            results = paginator.page(page)
        except EmptyPage:
            results = []

        serializer = self.get_serializer(results, many=True)
        response_data = {
            'data': serializer.data,
            'total': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page
        }
        # print('serializer.data:', serializer.data)
        return Response(response_data)


# class Phewas2ViewSet(viewsets.ModelViewSet):
#     serializer_class = Phewas2Serializer
#
#     def get_queryset(self):
#         queryset = Phewas.objects.all().order_by('id')
#         rsid = self.request.query_params.get('query', None)
#         if rsid:
#             queryset = queryset.filter(Q(snp=rsid))
#         return queryset
#
#     def list(self, request, *args, **kwargs):
#         queryset = self.get_queryset()
#         serializer = self.get_serializer(queryset, many=True)
#         return Response(serializer.data)
from django.db.models import FloatField, Case, When, Value

# class Phewas2ViewSet(viewsets.ModelViewSet):
#     serializer_class = Phewas2Serializer
#
#     def get_queryset(self):
#         queryset = Phewas.objects.all()
#
#         # 根据beta的正负和pval的大小进行排序
#         queryset = queryset.annotate(
#             beta_sign=Case(
#                 When(beta__gte=0, then=Value(1)),
#                 default=Value(-1),
#                 output_field=FloatField()
#             )
#         ).order_by('-beta_sign', 'pval')
#
#         rsid = self.request.query_params.get('query', None)
#         if rsid:
#             queryset = queryset.filter(Q(snp=rsid))
#         return queryset
#
#     def list(self, request, *args, **kwargs):
#         queryset = self.get_queryset()
#         serializer = self.get_serializer(queryset, many=True)
#         return Response(serializer.data)


class Phewas2ViewSet(viewsets.ModelViewSet):
    serializer_class = Phewas2Serializer

    def get_queryset(self):
        queryset = Phewas.objects.all()
        rsid = self.request.query_params.get('query', None)
        if rsid:
            queryset = queryset.filter(Q(snp=rsid))
        return queryset

    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        # 对查询结果进行排序
        sorted_queryset = sorted(queryset, key=lambda x: (-1 if x.beta >= 0 else 1, x.pval))
        serializer = self.get_serializer(sorted_queryset, many=True)
        return Response(serializer.data)


def get_csv_data(request):
    file_path = request.GET.get('filePath', None)
    file_path = file_path.replace('!', '/')
    if file_path is None:
        return JsonResponse({'error': 'File path is missing'}, status=400)

    try:
        base_dir = 'E:/mywork/table/MR_result_new3'  # 你的文件根目录
        full_path = os.path.join(base_dir, file_path, 'mr_result.csv')
        print('full_path:', full_path)
        with open(full_path, 'r') as csv_file:
            csv_reader = csv.DictReader(csv_file)
            csv_data = list(csv_reader)
            return JsonResponse({'csv_data': csv_data})
    except Exception as e:
        return JsonResponse({'error': str(e)}, status=500)
